1:LAMP环境的搭建方法: 第一种:RPM包安装方式 第二种:源代码包编译安装方式 https:443
http:80 客户端------WEB服务器------MYSQL数据库
当客户端发送请求时 WEB服务器通过PHP连接后台MYSQL数据库
同样:perl/java/python都可以用来连接后台的数据库
MYSQL有专门自己的存储集群 MYSQL的三种架构:单机/AB复制/集群 相关软件包:
[iyunv@stu15 ~]# rpm -qa | grep mysql
mysql-5.0.45-7.el5 客户端应用程序
mysql-server-5.0.45-7.el5 服务器端的应用程序
php-mysql-5.1.6-23.el5 php用来连接数据库的工具 当php被apache编译成模块时:
[iyunv@stu15 ~]# ls /etc/httpd/modules/libphp5.so
/etc/httpd/modules/libphp5.so 如果是采用rpm包安装的话,php有自己的配置文件:
[iyunv@stu15 conf.d]# pwd
/etc/httpd/conf.d
[iyunv@stu15 conf.d]# ls
php.conf proxy_ajp.conf README welcome.conf
叫php.conf
php是一种编程语言 MYSQL是一种关系型数据库管理系统(RDBMS)
MYSQL--databases--表--记录
数据库在文件系统里面都是以目录的形式存在的:/var/lib/mysql下(rpm包安装方式的情况下)
且下面的所有的目录都是700,默认情况下,系统中有两个数据库:mysql/test 其中test是用来测试用的,而mysql则是用来存放用户、表定义、索引之类的东东!而下面的mysql.sock是套接字文件,是用来和客户端连接用的,当mysql服务启动时就存在该文件,如果没启动,则没有!! 采用源码包搭建LAMP环境
相关软件包及文档:
[iyunv@stu15 lamp]# ls
AdobeReader_chs-7.0.0-2.i386.rpm
Discuz!_6.0.0_SC_GBK.pcpop.zip
httpd-2.2.11.tar.bz2
lamp.txt
mysql
mysql-5.1.34.tar.gz
mysql-cluster-gpl-6.3.20-linux-i686-glibc23.tar.gz
php-5.2.9.tar.bz2
phpBB
phpMyAdmin-3.1.4-all-languages.tar.bz2 现装apache
[iyunv@stu15 lamp]# tar jxvf httpd-2.2.11.tar.bz2 -C /usr/local/src
现解压,进入解压目录
[iyunv@stu15 httpd-2.2.11]# pwd
/usr/local/src/httpd-2.2.11
[iyunv@stu15 httpd-2.2.11]# ls
ABOUT_APACHE CHANGES include modules srclib
acinclude.m4 config.layout INSTALL NOTICE support
Apache.dsw configure InstallBin.dsp NWGNUmakefile test
apachenw.mcp.zip configure.in LAYOUT os VERSIONING
build docs libhttpd.dsp README
BuildAll.dsp emacs-style LICENSE README.platforms
BuildBin.dsp httpd.dsp Makefile.in ROADMAP
buildconf httpd.spec Makefile.win server
下面的INSTALL文件主要是用来告诉你如何安装的
[iyunv@stu15 httpd-2.2.11]# vim INSTALL
10 $ ./configure --prefix=PREFIX
11 $ make
12 $ make install
13 $ PREFIX/bin/apachectl start
也可以查看./configure的相关帮助
[iyunv@stu15 httpd-2.2.11]# ./configure --help
编译前的准备:
./configure --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-so --enable-rewrite --enable-ssl
编译:
make
安装:
make install
安装完成:
[iyunv@stu15 apache2]# pwd
/usr/local/apache2
[iyunv@stu15 apache2]# ls
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
下面的一系列目录
conf是放配置文件
htdocs是放网页的
logs是用来存放日志的
modules是用来存放模块的
[iyunv@stu15 bin]# pwd
/usr/local/apache2/bin
[iyunv@stu15 bin]# ls
ab apu-1-config dbmmanage htcacheclean htpasswd logresolve
apachectl apxs envvars htdbm httpd rotatelogs
apr-1-config checkgid envvars-std htdigest httxt2dbm
apache的启动文件:apachectl
测试文件:ab 启动服务:
[iyunv@stu15 bin]# ./apachectl start
httpd (pid 3163) already running
[iyunv@stu15 ~]# /usr/local/apache2/bin/apachectl restart
现在就可以通过浏览器来访问了
因为命令比较长,可以写一个简单的脚本来执行
如果想apache开机就启动,可以把该命令放到/etc/rc.d/rc.local里面
如何开启https的443端口呢?
首先在服务器端产生一对公私钥
产生证书请求:
[iyunv@stu15 ~]# mkdir k
[iyunv@stu15 ~]# cd k
[iyunv@stu15 k]# [iyunv@stu15 k]# openssl req -new -out server.csr
Generating a 1024 bit RSA private key
......++++++
...++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:beijing
Locality Name (eg, city) [Newbury]:
Organization Name (eg, company) [My Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []: Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[iyunv@stu15 k]# ls
privkey.pem server.csr
生成两个临时文件 生成私钥
[iyunv@stu15 k]# openssl rsa -in privkey.pem -out server.key
Enter pass phrase for privkey.pem:
writing RSA key
[iyunv@stu15 k]# ls
privkey.pem server.csr server.key 生成证书:
[iyunv@stu15 k]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365
Signature ok
subject=/C=CN/ST=beijing/L=Newbury/O=My Company Ltd
Getting Private key
[iyunv@stu15 k]# ls
privkey.pem server.crt server.csr server.key
我们想要的两个文件server.crt/server.key都有了,下面则将这两个文件防止不同的位置
在这之前我们得修改一下apache的主配置文件:
[iyunv@stu15 ~]# vim /usr/local/apache2/conf/httpd.conf
修改其445行 将其#号注释去掉
445 Include conf/extra/httpd-ssl.conf
保存退出
然后我们来查看一下:server.crt/server.key两个文件的放置位置帮助:
[iyunv@stu15 extra]# pwd
/usr/local/apache2/conf/extra
[iyunv@stu15 extra]# ls
httpd-autoindex.conf httpd-languages.conf httpd-ssl.conf
httpd-dav.conf httpd-manual.conf httpd-userdir.conf
httpd-default.conf httpd-mpm.conf httpd-vhosts.conf
httpd-info.conf httpd-multilang-errordoc.conf
查看httpd-ssl.conf
[iyunv@stu15 extra]# vim httpd-ssl.conf
99 SSLCertificateFile "/usr/local/apache2/conf/server.crt"
107 SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
进入/usr/local/apache2/conf目录下面
cp /root/k/server.key .
cp /root/k/server.crt . 重新启动apache服务
[iyunv@stu15 ~]# /usr/local/apache2/bin/apachectl restart
这样服务器的443端口就打开了
[iyunv@stu15 ~]# nmap 192.168.0.158
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2010-01-19 11:34 CST
Interesting ports on 192.168.0.158:
Not shown: 1673 closed ports
PORT STATE SERVICE
80/tcp open http
111/tcp open rpcbind
443/tcp open https
640/tcp open unknown
913/tcp open unknown
1000/tcp open cadlock
2049/tcp open nfs
Nmap finished: 1 IP address (1 host up) scanned in 13.108 seconds
测试: https://192.168.0.158 即可访问 直接把apache2目录删掉就可以把源码包安装的卸载
[iyunv@stu15 bin]# ./httpd -M
查看模块
源码安装MYSQL
[iyunv@stu15 ~]# tar zxvf mysql-5.1.34.tar.gz -C /usr/local/src
进入解压目录:
[iyunv@stu15 mysql-5.1.34]# pwd
/usr/local/src/mysql-5.1.34
[iyunv@stu15 mysql-5.1.34]# ls
aclocal.m4 COPYING ltmain.sh server-tools
BUILD dbug Makefile sql
ChangeLog depcomp Makefile.am sql-bench
client Docs Makefile.in sql-common
CMakeLists.txt EXCEPTIONS-CLIENT man storage
cmd-line-utils extra missing strings
config include mysql-test support-files
config.guess install-sh mysys tests
config.h.in INSTALL-SOURCE netware unittest
config.log INSTALL-WIN-SOURCE plugin vio
config.status libmysql pstack win
config.sub libmysqld README ylwrap
configure libmysql_r regex zlib
configure.in libtool scripts
编译前的准备:
[iyunv@stu15 mysql-5.1.34]# ./configure --with-mysqld-user=mysql --prefix=/usr/local/mysql --with-extra-charsets=all --exec-prefix=/usr/local/mysql --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-innodb --with-plugins=partition
编译:
make
安装:
make install 安装完成后的相关目录:
[iyunv@stu15 mysql]# pwd
/usr/local/mysql
[iyunv@stu15 mysql]# ls
bin docs include lib libexec mysql-test share sql-bench
[iyunv@stu15 bin]# ls
innochecksum mysql_client_test mysql_secure_installation
msql2mysql mysql_config mysql_setpermission
myisamchk mysql_convert_table_format mysqlshow
myisam_ftdump mysqld_multi mysqlslap
myisamlog mysqld_safe mysqltest
myisampack mysqldump mysql_tzinfo_to_sql
my_print_defaults mysqldumpslow mysql_upgrade
mysql mysql_find_rows mysql_waitpid
mysqlaccess mysql_fix_extensions mysql_zap
mysqladmin mysql_fix_privilege_tables perror
mysqlbinlog mysqlhotcopy replace
mysqlbug mysqlimport resolveip
mysqlcheck mysql_install_db resolve_stack_dump
现在我们来初始化MYSQL:
执行./mysql_install_db
默认情况/usr/local/mysql目录下没有var目录,当我们执行MYSQL初始化后在该目录下将会产生var目录
[iyunv@stu15 bin]# ./mysql_install_db
Installing MySQL system tables...
100119 13:35:28 [Warning] Forcing shutdown of 1 plugins
OK
Filling help tables...
100119 13:35:28 [Warning] Forcing shutdown of 1 plugins
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h stu15.uplooking.com password 'new-password'
Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/local/mysql/bin/mysqlbug script!
The latest information about MySQL is available at http://www.mysql.com/ Support MySQL by buying support/licenses from http://shop.mysql.com/ [iyunv@stu15 bin]# cd ..
[iyunv@stu15 mysql]# ls
bin docs include lib libexec mysql-test share sql-bench var
[iyunv@stu15 mysql]# cd var
[iyunv@stu15 var]# ls
mysql test 默认情况下var目录下存放的是缺省的数据库
如果MYSQL无法初始化的话,首先得查看系统中是否存在mysql用户,如果没有得创建
第二种情况就是mysql的rpm包惹的祸,我们可以将系统中原先安装的mysql的rpm包强制卸载
rpm -e mysql --nodeps 接下来我们得修改相关目录的权限
[iyunv@stu15 local]# pwd
/usr/local
[iyunv@stu15 local]# ll -d mysql/
drwxr-xr-x 11 root root 4096 01-19 13:35 mysql/
mysql目录的所属主和所属组都是root,进行如下修改:
[iyunv@stu15 local]# chown .mysql mysql/ -R
[iyunv@stu15 local]# ll -d mysql/
drwxr-xr-x 11 root mysql 4096 01-19 13:35 mysql/ 还有var目录
[iyunv@stu15 mysql]# pwd
/usr/local/mysql
[iyunv@stu15 mysql]# ll -d var
drwx------ 4 root mysql 4096 01-19 13:35 var
[iyunv@stu15 mysql]# chown mysql var -R
[iyunv@stu15 mysql]# ll -d var
drwx------ 4 mysql mysql 4096 01-19 13:35 var 接下来我们就来启动mysql
[iyunv@stu15 mysql]# ls
bin docs include lib libexec mysql-test share sql-bench var
[iyunv@stu15 mysql]# cd bin
[iyunv@stu15 bin]# ./mysqld_safe --user=mysql &
[1] 6374
[iyunv@stu15 bin]# 100119 13:50:20 mysqld_safe Logging to '/usr/local/mysql/var/stu15.uplooking.com.err'.
100119 13:50:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var [iyunv@stu15 bin]#
查看var目录下的变化:
[iyunv@stu15 var]# pwd
/usr/local/mysql/var
[iyunv@stu15 var]# ls
ibdata1 ib_logfile1 stu15.uplooking.com.err test
ib_logfile0 mysql stu15.uplooking.com.pid
查看一下进程:
[iyunv@stu15 var]# ps -e|grep mysql
6374 pts/1 00:00:00 mysqld_safe
6425 pts/1 00:00:00 mysqld
说明mysql已经启动,现在我们得测试进入MYSQL数据库
[iyunv@stu15 bin]# pwd
/usr/local/mysql/bin
[iyunv@stu15 bin]# ./mysql
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 1
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
修改MYSQL数据库密码:
[iyunv@stu15 bin]# ./mysqladmin -u root password 123
[iyunv@stu15 bin]# ./mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[iyunv@stu15 bin]# ./mysql -u root -p123
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
或者:
[iyunv@stu15 bin]# ./mysql -u root -p
Enter password:(在这里输入密码)
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 5
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql>
MYSQL默认情况下提供了一个启动脚本在如下目录下:
[iyunv@stu15 mysql]# pwd
/usr/local/mysql/share/mysql
[iyunv@stu15 mysql]# ll mysql.server
-rwxr-xr-x 1 root mysql 12087 01-19 12:08 mysql.server
让其成为独立服务,并使其可以用service ----- stop/start/restart来操作
[iyunv@stu15 mysql]# pwd
/usr/local/mysql/share/mysql
[iyunv@stu15 mysql]# cp mysql.server /etc/init.d/
[iyunv@stu15 mysql]# chkconfig --add mysql.server
[iyunv@stu15 mysql]# service mysql.server restart
[iyunv@stu15 ~]# service mysql.server restart
Shutting down MySQL. [确定]
Starting MySQL. [确定]
[iyunv@stu15 mysql]# chkconfig --list mysql.server
mysql.server 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
这个时候每次进入数据库都得进入mysql的安装目录的bin目录下执行命令:./mysql,为啥不进行从简呢?
[iyunv@stu15 ~]# which mysql
/usr/bin/which: no mysql in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin) vim /root/.bash_profile
10 PATH=$MYSQL_HOME:$PATH:$HOME/bin
11 MYSQL_HOME=/usr/local/mysql/bin
[iyunv@stu15 ~]# source .bash_profile
[iyunv@stu15 ~]# which mysql
/usr/local/mysql/bin/mysql
[iyunv@stu15 ~]# echo $PATH
/usr/local/mysql/bin::/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/root/bin:/root/bin
[iyunv@stu15 ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[iyunv@stu15 ~]# mysql -u root -p123
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 2
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> 如何为数据库修改密码呢?
[iyunv@stu15 ~]# mysqladmin -u root password 123456 -p123
[iyunv@stu15 ~]# mysql -u root -p123456
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> 介绍几种创建数据库的方法:
[iyunv@stu15 var]# ls
discuz ib_logfile0 mysql stu15.uplooking.com.err test
ibdata1 ib_logfile1 phpbb stu15.uplooking.com.pid
[iyunv@stu15 var]# mkdir db1
[iyunv@stu15 var]# chown mysql.mysql db1 -R
[iyunv@stu15 var]# chmod 700 db1 -R
[iyunv@stu15 var]# mysql -u root -p123456
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 5
Server version: 5.1.34 Source distribution
Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| discuz |
| mysql |
| phpbb |
| test |
+--------------------+
6 rows in set (0.08 sec)
mysql>
或者用:
[iyunv@stu15 var]# mysqladmin create db1 -u root -p123456
所以说创建数据库有三种方法。对应的删除数据库也同样有三种方法。