安裝MySQL數據庫
安裝步驟介紹
本例采用MySQL二進制安裝包進行安裝演示
(1) 創建mysql用戶的賬號
[root@mysql ~]# groupadd mysql[root@mysql ~]# useradd -s /sbin/nologin -g mysql -M mysql[root@localhost ~]# tail -1 /etc/passwdmysql:x:500:501::/home/mysql:/sbin/nologin[root@mysql ~]# id mysqluid=500(mysql) gid=501(mysql) groups=501(mysql)
(2)獲取MySQL二進制軟件包
百度云盤:http://pan.baidu.com/s/1hrBCzsC
提取碼:4yjf

(3) 采用二進制方式安裝MySQL
[root@mysql ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/[root@mysql ~]# cd /usr/local/[root@mysql local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32[root@mysql local]# ln -s mysql-5.5.32 mysql[root@mysql local]# lsbin etc games include lib lib64 libexec mysql mysql-5.5.32 sbin share src[root@mysql local]# cd /usr/local/mysql[root@mysql mysql]# lsbin data include lib mysql-test scripts sql-benchCOPYING docs INSTALL-BINARY man README share support-files#提示:二進制安裝包,僅需要解壓就可以了,不需要執行cmake/configure,make,make install等過程
(4)初始化MySQL配置文件my.cnf
[root@mysql mysql]# ls -l support-files/*.cnf-rw-r--r--. 1 7161 wheel 4691 Jun 19 2013 support-files/my-huge.cnf-rw-r--r--. 1 7161 wheel 19759 Jun 19 2013 support-files/my-innodb-heavy-4G.cnf-rw-r--r--. 1 7161 wheel 4665 Jun 19 2013 support-files/my-large.cnf-rw-r--r--. 1 7161 wheel 4676 Jun 19 2013 support-files/my-medium.cnf-rw-r--r--. 1 7161 wheel 2840 Jun 19 2013 support-files/my-small.cnf[root@mysql mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf
提示:
- support-files下有my.cnf的各種配置樣例。
- 使用cp全路徑/bin/cp,可實現拷貝而不出現替換提示,即如果有重名文件會直接覆蓋
- 本例為測試安裝環境,因此選擇參數配置小的my-small.cnf配置模版,如果是生產環境可以根據硬件選擇更高級的配置文件,上述配置文件模版對硬件的要求從低到高依次為
my-medium.cnf (最低)my-small.cnfmy-large.cnfmy-huge.cnfmy-innodb-heavy-4G.cnf(最高)
(5)初始化MySQL數據庫文件
初始化命令如下
[root@mysql mysql]# mkdir -p /usr/local/mysql/data[root@mysql mysql]# chown -R mysql.mysql /usr/local/mysql[root@mysql mysql]# yum -y install libaio[root@mysql mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql#初始化MySQL數據庫文件,會有很多信息提示,如果沒有ERROR級別的錯誤,會有兩個OK的字樣,表示初始化成功,否則就要解決初始化的問題
配置并啟動MySQL數據庫
(1)設置MySQL啟動腳本,命令如下
[root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld#拷貝MySQL啟動腳本到MySQL的命令路徑[root@mysql mysql]# chmod +x /etc/init.d/mysqld#使腳本可執行
(2)MySQL二進制默認安裝路徑是/usr/local/mysql,啟動腳本里是/usr/local/mysql。如果安裝路徑不同,那么腳本里路徑等都需要替換
(3)啟動MySQL數據庫,命令如下:
[root@mysql mysql]# /etc/init.d/mysqld startStarting MySQL... SUCCESS![root@mysql mysql]# netstat -antup | grep mysqltcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1347/mysqld
如果發現3306端口沒起來,請tail -100 /usr/local/mysql/data/主機名.err查看日志信息,看是否有報錯信息,然后根據相關錯誤提示進行調試。經常查看服務運行日志是個很好的習慣,也是高手的習慣。
(4)設置MySQL開機自啟動,命令如下:
[root@mysql mysql]# chkconfig --add mysqld[root@mysql mysql]# chkconfig mysqld on[root@mysql mysql]# chkconfig --list mysqldmysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
(5)配置mysql命令的全局使用路徑,命令如下:
[root@mysql mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/[root@mysql mysql]# which mysqladmin/usr/local/bin/mysqladmin
(6)登陸MySQL測試,命令如下:
[root@mysql mysql]# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.32 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL安全配置
(1)為MySQL的root用戶設置密碼,命令如下:
[root@mysql mysql]# mysqladmin -uroot password '666666'[root@mysql mysql]# mysql -uroot -p666666Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.5.32 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(2)清理無用的MySQL用戶及庫,命令如下:
mysql> select user,host from mysql.user;+------+-----------+| user | host |+------+-----------+| root | 127.0.0.1 || root | ::1 || | localhost || root | localhost || | mysql || root | mysql |+------+-----------+6 rows in set (0.00 sec)mysql> drop user "root"@"::1";Query OK, 0 rows affected (0.00 sec)mysql> drop user ""@"localhost";Query OK, 0 rows affected (0.05 sec)mysql> select user,host from mysql.user;+------+-----------+| user | host |+------+-----------+| root | 127.0.0.1 || root | localhost || | mysql || root | mysql |+------+-----------+4 rows in set (0.00 sec)
安裝nginx
nginx的編譯安裝部署
[root@nginx ~]# mount /dev/sr0 /media/cdrom/mount: block device /dev/sr0 is write-protected, mounting read-only[root@nginx ~]# yum install -y pcre-devel openssl-devel#wget -q http://nginx.org/download/nginx-1.10.2.tar.gz[root@nginx ~]# useradd -s /sbin/nologin -M www #創建程序用戶[root@nginx ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解壓縮[root@nginx ~]# cd /usr/src/nginx-1.10.2[root@nginx nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #預配置[root@nginx nginx-1.10.2]# make && make install #編譯和安裝[root@nginx nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #給命令做軟連接,以便PATH能找到[root@nginx nginx-1.10.2]# /usr/local/nginx/sbin/nginx #啟動nginx
配置nginx配置文件
[root@nginx conf]# cat nginx.conf.default | egrep -v "#|^$" > nginx.conf[root@nginx conf]# vim nginx.conf[root@nginx conf]# cat nginx.confworker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name www.yunjisuan.com;root /www;location / {index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}# 檢查配置文件[root@nginx conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful#加入映射文件[root@nginx conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts[root@nginx conf]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.200.20 www.yunjisuan.com
添加網頁
[root@nginx conf]# mkdir /www[root@nginx conf]# echo "`hostname -I` www.yunjisuan.com" >> /www/index.html[root@nginx conf]# chown -R www.www /www/ #PHP的程序用戶也要弄成www[root@nginx conf]# curl www.yunjisuan.com192.168.200.20 www.yunjisuan.com
安裝PHP
檢查安裝PHP所需的lib庫
PHP程序在開發及運行時會調用一些諸如zlib,gd等函數庫,因此需要確認lib庫是否已經安裝,執行過程如下:
[root@PHP ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel[root@PHP ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
提示:
1)每個lib一般都會存在對應的以“*-devel”命名的包,安裝lib對應的-devel包后,對應的lib包就會自動安裝好,例如安裝gd-devel時就會安裝gd。
2)這些lib庫不是必須安裝的,但是目前的企業環境下一般都需要安裝。否則,PHP程序運行時會出現問題,例如驗證碼無法顯示等。
執行下面命令安裝相關的lib軟件包
[root@PHP ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel[root@PHP ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
安裝后的結果如下:
[root@PHP ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devellibxml2-devel-2.7.6-14.el6.x86_64zlib-devel-1.2.3-29.el6.x86_64libjpeg-turbo-devel-1.2.1-1.el6.x86_64#這里僅缺少libiconv-devel包[root@PHP ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devellibxslt-devel-1.1.26-2.el6_3.1.x86_64libcurl-devel-7.19.7-37.el6_4.x86_64libpng-devel-1.2.49-1.el6_2.x86_64gd-2.0.35-11.el6.x86_64freetype-devel-2.3.11-14.el6_3.1.x86_64
從以上結果看出,僅有libiconv-devel這個包沒有安裝,因為默認的yum源沒有此包,后面會編譯安裝。
安裝yum無法安裝的libiconv庫
[root@PHP ~]# yum -y install wget[root@PHP ~]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz[root@PHP ~]# lsanaconda-ks.cfg install.log install.log.syslog libiconv-1.14.tar.gz[root@PHP ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/[root@PHP ~]# cd /usr/src/libiconv-1.14/[root@PHP libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
安裝libmcrypt庫
推薦使用簡單的在線yum的方式安裝:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo[root@PHP libiconv-1.14]# yum -y install libmcrypt-devel
安裝mhash加密擴展庫
推薦使用簡單的在線yum的方式安裝:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo[root@PHP ~]# yum -y install mhash
安裝mcrvpt加密擴展庫
推薦使用簡單的在線yum的方式安裝:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo[root@PHP ~]# yum -y install mcrypt
開始安裝PHP(FastCGI方式)服務
獲取PHP軟件包
[root@PHP ~]# wget http://cn2.php.net/get/php-5.3.28.tar.gz/from/this/mirror
解壓配置PHP
[root@PHP ~]# useradd -s /sbin/nologin -M www[root@PHP ~]# id wwwuid=500(www) gid=500(www) groups=500(www)[root@PHP ~]# tar xf php-5.3.28.tar.gz -C /usr/src/[root@PHP ~]# cd /usr/src/php-5.3.28/[root@PHP php-5.3.28]# ./configure --prefix=/usr/local/php5.3.28 --with-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp
-devel是安裝前面命令有關的所有包
yum install openssl openssl-devel
執行上述命令后,最后的正確輸出提示為下圖 
編譯PHP
正確執行前文配置PHP軟件的./configure系列命令后,就可以編譯PHP軟件了,具體操作過程如下:
[root@PHP php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/ #分離式不用這步[root@PHP php-5.3.28]# touch ext/phar/phar.phar #分離式不用這步[root@PHP php-5.3.28]# make # 直接編譯#make最后的正確提示Build complete.Don't forget to run 'make test'.
安裝PHP生成文件到系統
[root@PHP php-5.3.28]# make install
配置PHP引擎配置文件php.ini
(1)設置軟鏈接以方便訪問,命令如下
[root@PHP php-5.3.28]# ln -s /usr/local/php5.3.28/ /usr/local/php[root@PHP php-5.3.28]# ls -l /usr/local/phplrwxrwxrwx. 1 root root 21 Nov 25 23:30 /usr/local/php -> /usr/local/php5.3.28/
(2)查看PHP配置默認模版文件,命令如下
[root@PHP php-5.3.28]# ls php.ini*php.ini-development php.ini-production
(3)拷貝PHP配置文件到PHP默認目錄,并更改文件名稱為php.ini,命令如下
[root@PHP php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini[root@PHP php-5.3.28]# ls -l /usr/local/php/lib/php.ini-rw-r--r--. 1 root root 69627 Nov 25 23:32 /usr/local/php/lib/php.ini
配置PHP(FastCGI方式)的配置文件php-fpm.conf
[root@PHP php-5.3.28]# cd /usr/local/php/etc/[root@PHP etc]# lspear.conf php-fpm.conf.default[root@PHP etc]# cp php-fpm.conf.default php-fpm.conf#修改配置文件就是修改一下監聽端口

啟動PHP服務(FastCGI方式)
(1)啟動PHP服務php-fpm,命令如下
[root@PHP etc]# /usr/local/php/sbin/php-fpm
(2)檢查PHP服務php-fpm的進程及啟動端口的情況,命令如下:
[root@PHP etc]# ps -ef | grep php-fpmroot 30699 1 0 23:42 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf)www 30700 30699 0 23:42 ? 00:00:00 php-fpm: pool wwwwww 30701 30699 0 23:42 ? 00:00:00 php-fpm: pool wwwroot 30716 1046 0 23:43 pts/0 00:00:00 grep php-fpm[root@PHP etc]# lsof -i:9000COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEphp-fpm 30699 root 7u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)php-fpm 30700 www 0u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)php-fpm 30701 www 0u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
配置Nginx支持PHP程序請求訪問
修改Nginx配置文件
[root@nginx conf]# cat nginx.confworker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name www.yunjisuan.com;root /www;location / {index index.html index.htm;}location ~ .*\.(php|php5)?$ {fastcgi_pass 192.168.200.30:9000;fastcgi_index index.php;include fastcgi.conf;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}}[root@nginx conf]# /usr/local/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@nginx conf]# /usr/local/sbin/nginx -s reload
測試
映射IP 

測試動態
[root@nginx conf]# cd /www/[root@nginx www]# lsindex.html[root@nginx www]# echo "huahua" > index.php[root@nginx www]# lsindex.html index.php[root@PHP etc]# mkdir /www #php服務器里的目錄必須和nginx服務器的一致[root@PHP www]# touch index.php[root@PHP www]# echo "123" >> index.php[root@PHP www]# cat index.php123

加入數據庫
[root@mysql mysql]# mysql -uroot -p666666Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.5.32 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> grant all on *.* to 'yunjisuan'@'%' identified by '666666';Query OK, 0 rows affected (0.00 sec)
創建網頁
#php上創建[root@PHP www]# cat test_mysql.php<?php//$link_id=mysql_connect('主機名','用戶','密碼');$link_id=mysql_connect('192.168.200.31','yunjisuan','666666');if($link_id){echo "mysql successful by hua !";}else{echo mysql_error();}?>#nginx也要創建
測試

浙公網安備 33010602011771號