Linux實戰教學筆記28:企業級LNMP環境應用實踐
一,LNMP應用環境
1.1 LNMP介紹
大約在2010年以前,互聯網公司最常用的經典Web服務環境組合就是LAMP(即Linux,Apache,MySQL,PHP),近幾年隨着Nginx Web服務的逐漸流行,又出現了新的Web服務環境組合--LNMP或LEMP,其中LNMP為Linux,Nginx,MySQL,PHP等首字母的縮寫,而LEMP中的E則表示Nginx,它取自Nginx名字的發音(engine x)。現在,LNMP已經逐漸成為國內大中型互聯網公司網站的主流組合環境,因此,我們必須熟練掌握LNMP環境的搭建,優化及維護方法。
1.2 LNMP組合工作流程
在深入學習LNMP組合之前,有必要先來了解以下LNMP環境組合的基本原理,也就是它們之間到底是怎樣互相調度的?
在LNMP組合工作時,首先是用戶通過瀏覽器輸入域名請求Nginx Web服務,如果請求是靜態資源,則由Nginx解析返回給用戶;如果是動態請求(.php結尾),那么Nginx就會把它通過FastCGI接口(生產常用方法)發送給PHP引擎服務(FastCGI進程php-fpm)進行解析,如果這個動態請求要讀取數據庫數據,那么PHP就會繼續向后請求MySQL數據庫,以讀取需要的數據,並最終通過Nginx服務把獲取的數據返回給用戶,這就是LNMP環境的基本請求順序流程。這個請求流程是企業使用LNMP環境的常用流程。
二,LNMP之MySQL數據庫
2.1 MySQL數據庫介紹
MySQL是互聯網領域里非常重要的,深受廣大用戶歡迎的一款開源關系型數據庫軟件,由瑞典MySQL AB公司開發與維護。2006年,MySQL AB公司被SUN公司收購,2008年,SUN公司又被傳統數據數據庫領域大佬甲骨文(Oracle)公司收購。因此,MySQL數據庫軟件目前屬於Oracle公司,但扔是開源的,Oracle公司收購MySQL的戰略意圖顯而易見,其自身的Oracle數據庫繼續服務於傳統大中型企業,而利用收購的MySQL搶占互聯網領域數據庫份額,完成其戰略布局。
MySQL是一種關系型數據庫管理軟件,關系型數據庫的特點是將數據保存在不同的二維表中,並且將這些表放入不同的數據庫中,而不是把所有數據統一放在一個大倉庫里,這樣的設計增加了MySQL的讀取速度,靈活性和可管理性也得到了很大提高。訪問及管理MySQL數據庫的最常用標准化語言為SQL結構化查詢語言。
2.2 為什么選擇MySQL數據庫
目前,絕大多數使用Linux操作系統的互聯網企業都使用MySQL作為后端的數據庫,從大型的BAT門戶,到電商門戶平台,分類門戶平台等無一例外。那么,MySQL數據庫到底有哪些優勢和特點,讓大家毫不猶豫的選擇它呢?
原因可能有以下幾點
- 性能卓越,服務穩定,很少出現異常宕機。
- 開放源代碼且無版權制約,自主性強,使用成本低。
- 歷史悠久,社區及用戶非常活躍,遇到問題,可以很快獲取到幫助。
- 軟件體積小,安裝使用簡單,並且易於維護,安裝及維護成本低。
- 支持多種操作系統,提供多種API接口,支持多種開發語言,特別是對流行的PHP語言無縫支持。
- 品牌口碑效應,使得企業無需考慮就直接用之。
2.3 安裝MySQL數據庫
2.3.1 安裝概覽
MySQL有幾種不同的產品線,且每種產品線又有很多不同的版本,這里選擇當前企業使用最廣的社區版MySQL5.5系列作為LNMP的組合環境數據庫平台。
MySQL的安裝方法也有很多,常見的方法如下圖所示:
備注:安裝MySQL的注意事項如下:
(1)建議和之前介紹的Nginx服務安裝在同一台機器上。
(2)重視操作過程的報錯輸出,有錯誤要解決掉再繼續,不能忽略編譯中的錯誤。
2.3.2 安裝步驟介紹
本例采用MySQL二進制安裝包進行安裝演示
(1) 創建mysql用戶的賬號
[root@localhost ~]# groupadd mysql [root@localhost ~]# useradd -s /sbin/nologin -g mysql -M mysql [root@localhost ~]# tail -1 /etc/passwd mysql:x:501:501::/home/mysql:/sbin/nologin [root@localhost ~]# id mysql uid=501(mysql) gid=501(mysql) groups=501(mysql)
(2)獲取MySQL二進制軟件包
百度雲盤:http://pan.baidu.com/s/1hrBCzsC
提取碼:4yjf
(3) 采用二進制方式安裝MySQL
[root@localhost ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/ [root@localhost ~]# cd /usr/local/ [root@localhost local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32 [root@localhost local]# ln -s mysql-5.5.32 mysql [root@localhost local]# ls bin games lib libexec mysql-5.5.32 nginx-1.10.2 share etc include lib64 mysql nginx sbin src [root@localhost local]# cd /usr/local/mysql [root@localhost mysql]# ls bin data include lib mysql-test scripts sql-bench COPYING docs INSTALL-BINARY man README share support-files #提示: 二進制安裝包,僅需要解壓就可以了,不需要執行cmake/configure,make,make install等過程
- [x] :當安裝LNMP一體化環境時,MySQL數據庫要裝在Nginx所在的機器上。如果MySQL和Nginx不在一台機器上,那么,Nginx服務器上的MySQL數據庫軟件包只要解壓移動到/usr/local/目錄,改名為mysql就可以了,不需要進行后面的初始化配置。
- [x] :在非一體的LNMP環境(Nginx和MySQL不在一台機器上),編譯PHP環境時,也是需要MySQL數據庫環境的,但是高版本的PHP,例如5.3版本以上,內置了PHP需要的MySQL程序,因此,對於此類版本就不需要在Nginx服務器上安裝MySQL軟件了,只需要在編譯PHP時指定相關參數即可。這個PHP的編譯參數為--with-mysql=mysqld,表示PHP程序在編譯時會調用內置的MySQL的庫。
(4)初始化MySQL配置文件my.cnf
命令如下:
[root@localhost ~]# cd /usr/local/mysql [root@localhost 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@localhost 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.cnf my-large.cnf my-huge.cnf my-innodb-heavy-4G.cnf(最高)
(5)初始化MySQL數據庫文件
初始化命令如下:
[root@localhost ~]# mkdir -p /usr/local/mysql/data #建立MySQL數據文件目錄 [root@localhost ~]# chown -R mysql.mysql /usr/local/mysql #授權mysql用戶管理MySQL的安裝目錄 [root@localhost ~]# yum -y install libaio #光盤源安裝依賴包,否則下一步的編譯會報錯 [root@localhost ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql #初始化MySQL數據庫文件,會有很多信息提示,如果沒有ERROR級別的錯誤,會有兩個OK的字樣,表示初始化成功,否則就要解決初始化的問題 初始化內容如下: Installing MySQL system tables... OK Filling help tables... 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 localhost 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/scripts/mysqlbug script!
以上的命令主要作用是生成如下數據庫文件
[root@localhost ~]# tree /usr/local/mysql/data/ /usr/local/mysql/data/ ├── mysql │ ├── columns_priv.frm │ ├── columns_priv.MYD │ ├── columns_priv.MYI │ ├── db.frm │ ├── db.MYD │ ├── db.MYI │ ├── event.frm │ ├── event.MYD │ ├── event.MYI │ ├── func.frm │ ├── func.MYD │ ├── func.MYI │ ├── general_log.CSM │ ├── general_log.CSV │ ├── general_log.frm │ ├── help_category.frm │ ├── help_category.MYD │ ├── help_category.MYI │ ├── help_keyword.frm ...以下省略若干...
這些MySQL數據文件是MySQL正確運行所必需的基本數據庫文件,其功能是對MySQL權限,狀態等進行管理。
2.3.3 初始化故障排錯集錦
錯誤示例1:
usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared ob #錯誤原因是沒有libaio函數庫的支持。需要 yum -y install libaio
錯誤示例2:
WARNING:The host'mysql'could not be looked up with resolveip #需要修改主機名解析,使其和uname -n一樣,修改后的結果如下: [root@localhost ~] # grep `uname -n` /etc/hosts
錯誤示例3:
ERROR:1004Can't create file '/tmp/#sql300e_1_o.frm'(errno:13) #原因是/tmp目錄的權限有問題。 解決辦法為處理/tmp目錄,如下: [root@localhost ~]# ls -ld /tmp drwxrwxrwt. 3 root root 4096 Jul 14 07:56 /tmp [root@localhost ~]# chmod -R 1777 /tmp/
此故障必須解除,否則,后面會出現登陸不了數據庫等問題。
2.4 配置並啟動MySQL數據庫
(1)設置MySQL啟動腳本,命令如下:
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld #拷貝MySQL啟動腳本到MySQL的命令路徑 [root@localhost mysql]# chmod +x /etc/init.d/mysqld #使腳本可執行
(2)MySQL二進制默認安裝路徑是/usr/local/mysql,啟動腳本里是/usr/local/mysql。如果安裝路徑不同,那么腳本里路徑等都需要替換
(3)啟動MySQL數據庫,命令如下:
[root@localhost mysql]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS!
以上是啟動數據庫的規范方法之一,但還可以用如下方式啟動,
/usr/local/mysql/bin/mysqld_safe --user=mysql &
這個命令結尾的“&”符號,作用是在后台執行MySQL服務,命令執行完還需要按下回車才能進入命令行狀態。
(4)檢查MySQL數據庫是否啟動,命令如下:
[root@localhost mysql]# netstat -antup | grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1702/mysqld
如果發現3306端口沒起來,請tail -100 /usr/local/mysql/data/主機名.err查看日志信息,看是否有報錯信息,然后根據相關錯誤提示進行調試。經常查看服務運行日志是個很好的習慣,也是高手的習慣。
(5)查看MySQL數據庫啟動結果日志,命令如下:
[root@localhost mysql]# tail -10 /usr/local/mysql/data/localhost.err InnoDB: Creating foreign key constraint system tables InnoDB: Foreign key constraint system tables created 170714 8:33:47 InnoDB: Waiting for the background threads to start 170714 8:33:48 InnoDB: 5.5.32 started; log sequence number 0 170714 8:33:48 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306 170714 8:33:48 [Note] - '0.0.0.0' resolves to '0.0.0.0'; 170714 8:33:48 [Note] Server socket created on IP: '0.0.0.0'. 170714 8:33:49 [Note] Event Scheduler: Loaded 0 events 170714 8:33:49 [Note] /usr/local/mysql/bin/mysqld: ready for connections. Version: '5.5.32' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
(6)設置MySQL開機自啟動,命令如下:
[root@localhost mysql]# chkconfig --add mysqld [root@localhost mysql]# chkconfig mysqld on [root@localhost mysql]# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
提示:也可以將啟動命令/etc/init.d/mysqld start 放到/etc/rc.local里面
(7)配置mysql命令的全局使用路徑,命令如下:
[root@localhost mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/ [root@localhost mysql]# which mysqladmin /usr/local/bin/mysqladmin
(8)登陸MySQL測試,命令如下:
[root@localhost mysql]# mysql #直接輸入命令即可登陸
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server 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 its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; #查看當前所有的數據庫 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> select user(); #查看當前的登陸用戶 +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) mysql> quit Bye
提示:
MySQL安裝完成以后,默認情況下,root賬戶是無密碼的,這個必須要設置。
2.5 MySQL安全配置
(1)為MySQL的root用戶設置密碼,命令如下:
[root@localhost mysql]# mysqladmin -u root password '123123' #設置密碼 [root@localhost mysql]# mysql #無法直接登陸了 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@localhost mysql]# mysql -uroot -p #新的登陸方式 Enter password: #輸入設置的密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server 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 its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
(2)清理無用的MySQL用戶及庫,命令如下:
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | +------+-----------+ 4 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.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
三,FastCGI介紹
3.1 什么是CGI
- CGI的全稱為“通用網關接口”(Common Gateway Interface),為HTTP服務器與其他機器上的程序服務通信交流的一種工具,CGI程序須運行在網絡服務器上。
- 傳統CGI接口方式的主要缺點是性能較差,因為每次HTTP服務器遇到動態程序時都需要重新啟動解析器來執行解析,之后結果才會被返回給HTTP服務器。這在處理高並發訪問時幾乎是不可用的,因此就誕生了FastCGI。另外,傳統的CGI接口方式安全性也很差,故而現在已經很少被使用了。
3.2 什么是FastCGI
FastCGI是一個可伸縮的,高速地在HTTP服務器和動態腳本語言間通信的接口(在Linux下,FastCGI接口即為socket,這個socket可以是文件socket,也可以是IP socket),主要優點是把動態語言和HTTP服務器分離出來。多數流行的HTTP服務器都支持FastCGI,包括Apache,Nginx和Lighttpd等。
同時,FastCGI也被許多腳本語言所支持,例如當前比較流程的腳本語言PHP。FastCGI接口采用的是C/S架構,它可以將HTTP服務器和腳本解析服務器分開,同時還能在腳本解析服務器上啟動一個或多個腳本來解析守護進程。當HTTP服務器遇到動態程序時,可以將其直接交付給FastCGI進程來執行,然后將得到的結果返回給瀏覽器。這種方式可以讓HTTP服務器專一地處理靜態請求,或者將動態腳本服務器的結果返回給客戶端,這在很大程度上提高了整個應用系統的性能。
FastCGI的重要特點如下:
- HTTP服務器和動態腳本語言間通信的接口或工具。
- 可把動態語言解析和HTTP服務器分離開。
- Nginx,Apache,Lighttpd,以及多數動態語言都支持FastCGI。
- FastCGI接口方式采用C/S結構,分為客戶端(HTTP服務器)和服務器端(動態語言解析服務器)
- PHP動態語言服務器端可以啟動多個FastCGI的守護進程(例如php-fpm(fcgi process mangement))
- HTTP服務器通過(例如Nginx fastcgi_pass)FastCGI客戶端和動態語言FastCGI服務器端通信(例如php-fpm)
3.3 Nginx FastCGI的運行原理
Nginx不支持對外部動態程序的直接調用或者解析,所有的外部程序(包括PHP)必須通過FastCGI接口來調用。FastCGI接口在Linux下是socket,為了調用CGI程序,還需要一個FastCGI的wrapper(可以理解為用於啟動另一個程序的程序),這個wrappper綁定在某個固定的socket上,如端口或文件socket。當Nginx將CGI請求發送給這個socket的時候,通過FastCGI接口,wrapper接收到請求,然后派生出一個新的線程,這個線程調用解釋器或外部程序處理腳本來讀取返回的數據;接着,wrapper再將返回的數據通過FastCGI接口,沿着固定的socket傳遞給Nginx;最后,Nginx將返回的數據發送給客戶端,這就是Nginx+FastCGI的整個運作過程。
FastCGI的主要優點是把動態語言和HTTP服務器分離開來,使Nginx專門處理靜態請求及向后轉發的動態請求,而PHP/PHP-FPM服務器則專門解析PHP動態請求。
3.4 LNMP之PHP(FastCGI方式)服務的安裝和准備
3.4.1 檢查Nginx及MySQL的安裝情況
(1)檢查確認Nginx及MySQL的安裝路徑,命令如下:
[root@localhost ~]# ls -ld /usr/local/nginx lrwxrwxrwx. 1 root root 24 Jul 9 14:31 /usr/local/nginx -> /usr/local/nginx-1.10.2/ [root@localhost ~]# ls -ld /usr/local/mysql lrwxrwxrwx. 1 mysql mysql 12 Jul 14 07:13 /usr/local/mysql -> mysql-5.5.32
(2)檢查端口及啟動情況,命令如下:
[root@localhost ~]# netstat -antup | grep -E "80|3306" tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1193/nginx tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1702/mysqld
(3)測試訪問Nginx及MySQL是否OK,命令如下:
[root@localhost ~]# wget 127.0.0.1 #測試Nginx --2017-07-14 09:54:12-- http://127.0.0.1/ Connecting to 127.0.0.1:80... connected. HTTP request sent, awaiting response... 200 OK Length: 624 [text/html] Saving to: “index.html” 100%[=========================================================================================>] 624 --.-K/s in 0s 2017-07-14 09:54:12 (2.12 MB/s) - “index.html” saved [624/624] [root@localhost ~]# mysql -uroot -p #測試MySQL Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server 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 its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit Bye
如果訪問結果和上述一致,就表明Nginx及MySQL的安裝一切正常
3.4.2 檢查安裝PHP所需的lib庫
PHP程序在開發及運行時會調用一些諸如zlib,gd等函數庫,因此需要確認lib庫是否已經安裝,執行過程如下:
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel zlib-devel-1.2.3-29.el6.x86_64 [root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
提示:
- [x] :每個lib一般都會存在對應的以“*-devel”命名的包,安裝lib對應的-devel包后,對應的lib包就會自動安裝好,例如安裝gd-devel時就會安裝gd。
- [x] :這些lib庫不是必須安裝的,但是目前的企業環境下一般都需要安裝。否則,PHP程序運行時會出現問題,例如驗證碼無法顯示等。
執行下面命令安裝相關的lib軟件包
[root@localhost ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel [root@localhost ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
安裝后的結果如下:
[root@localhost ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel zlib-devel-1.2.3-29.el6.x86_64 libxml2-devel-2.7.6-14.el6.x86_64 libjpeg-turbo-devel-1.2.1-1.el6.x86_64 #這里僅缺少libiconv-devel包 [root@localhost ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel freetype-devel-2.3.11-14.el6_3.1.x86_64 libpng-devel-1.2.49-1.el6_2.x86_64 libcurl-devel-7.19.7-37.el6_4.x86_64 libxslt-devel-1.1.26-2.el6_3.1.x86_64 gd-2.0.35-11.el6.x86_64
從以上結果看出,僅有libiconv-devel這個包沒有安裝,因為默認的yum源沒有此包,后面會編譯安裝。
3.4.3 安裝yum無法安裝的libiconv庫
[root@localhost ~]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz [root@localhost ~]# ls anaconda-ks.cfg install.log libiconv-1.14.tar.gz nginx-1.10.2.tar.gz index.html install.log.syslog mysql-5.5.32-linux2.6-x86_64.tar.gz [root@localhost ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/ [root@localhost ~]# cd /usr/src/libiconv-1.14/ [root@localhost libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
3.4.4 安裝libmcrypt庫
推薦使用簡單的在線yum的方式安裝:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
編譯安裝過程略
[root@localhost yum.repos.d]# yum -y install libmcrypt-devel
3.4.5 安裝mhash加密擴展庫
推薦使用簡單的在線yum的方式安裝:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
編譯安裝過程略
[root@localhost yum.repos.d]# yum -y install mhash
3.4.6 安裝mcrvpt加密擴展庫
推薦使用簡單的在線yum的方式安裝:wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
編譯安裝過程略
[root@localhost yum.repos.d]# yum -y install mcrypt
3.5 開始安裝PHP(FastCGI方式)服務
3.5.1 獲取PHP軟件包
[root@localhost ~]# wget http://cn2.php.net/get/php-5.3.28.tar.gz/from/this/mirror
3.5.2 解壓配置PHP
[root@localhost ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ./configure \
> --prefix=/usr/local/php5.3.28 \ > --with-mysql=/usr/local/mysql \ > --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 #特別強調:上述每行結尾的換行符反斜線(\)之后不能再有任何字符包括空格
執行上述命令后,最后的正確輸出提示為下圖
對於上面命令,部分參數說明如下:
- [x] :--prefix=/usr/local/php5.2.28
表示指定PHP的安裝路徑為/usr/local/php5.3.28
- [x] :--with-mysql=/usr/local/mysql
表示需要指定MySQL的安裝路徑,安裝PHP需要的MySQL相關內容。當然,如果沒有MySQL軟件包,也可以不單獨安裝,這樣的情況可使用--with-mysql=mysqlnd替代--with-mysql=/usr/local/mysql,因為PHP軟件里已經自帶了連接MySQL的客戶端工具。
- [x] :--with-fpm-user=www
nginx表示指定PHP-FPM進程管理的用戶為www,此處最好和Nginx服務用戶統一
- [x] : --with-fpm-group=www
表示指定PHP-FPM進程管理的組為www,此處最好與Nginx服務用戶組統一。
- [x] :--enable-fpm
表示激活PHP-FPM方式服務,即以FastCGIF方式運行PHP服務。
3.5.3 編譯PHP
正確執行前文配置PHP軟件的./configure系列命令后,就可以編譯PHP軟件了,具體操作過程如下:
[root@localhost php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 libmysqlclient.so.18 libmysqlclient.so.18.0.0 [root@localhost php-5.3.28]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/ [root@localhost php-5.3.28]# touch ext/phar/phar.phar [root@localhost php-5.3.28]# make #make最后的正確提示 Build complete. Don't forget to run 'make test'.
3.5.4 安裝PHP生成文件到系統
[root@localhost php-5.3.28]# make install
3.5.5 配置PHP引擎配置文件php.ini
(1)設置軟鏈接以方便訪問,命令如下:
[root@localhost ~]# ln -s /usr/local/php5.3.28/ /usr/local/php [root@localhost ~]# ls -l /usr/local/php lrwxrwxrwx. 1 root root 21 Jul 14 13:06 /usr/local/php -> /usr/local/php5.3.28/
(2)查看PHP配置默認模版文件,命令如下:
[root@localhost ~]# cd /usr/src/php-5.3.28/ [root@localhost php-5.3.28]# ls php.ini* php.ini-development php.ini-production
請注意以上兩文件的異同之處,可通過diff或vimdiff命令比較,如下圖所示:
從對比結果可以看出,開發環境更多的是開啟日志,調試信息,而生產環境都是關閉狀態
(3)拷貝PHP配置文件到PHP默認目錄,並更改文件名稱為php.ini,命令如下:
[root@localhost php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini [root@localhost php-5.3.28]# ls -l /usr/local/php/lib/php.ini -rw-r--r--. 1 root root 69627 Jul 14 13:25 /usr/local/php/lib/php.ini
3.5.6 配置PHP(FastCGI方式)的配置文件php-fpm.conf
[root@localhost php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini [root@localhost php-5.3.28]# ls -l /usr/local/php/lib/php.ini -rw-r--r--. 1 root root 69627 Jul 14 13:25 /usr/local/php/lib/php.ini [root@localhost php-5.3.28]# cd /usr/local/php/etc/ [root@localhost etc]# ls pear.conf php-fpm.conf.default [root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
關於php-fpm.conf,暫時可用默認的配置,先把服務搭好,以后再進行優化。
3.5.7 啟動PHP服務(FastCGI方式)
(1)啟動PHP服務php-fpm,命令如下:
[root@localhost etc]# /usr/local/php/sbin/php-fpm
(2)檢查PHP服務php-fpm的進程及啟動端口的情況,命令如下:
[root@localhost etc]# ps -ef | grep php-fpm root 126611 1 0 13:36 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf) nginx 126612 126611 0 13:36 ? 00:00:00 php-fpm: pool www nginx 126613 126611 0 13:36 ? 00:00:00 php-fpm: pool www root 126619 126548 0 13:39 pts/1 00:00:00 grep php-fpm [root@localhost etc]# lsof -i:9000 #默認9000端口提供服務 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME php-fpm 126611 root 7u IPv4 136041 0t0 TCP localhost:cslistener (LISTEN) php-fpm 126612 nginx 0u IPv4 136041 0t0 TCP localhost:cslistener (LISTEN) php-fpm 126613 nginx 0u IPv4 136041 0t0 TCP localhost:cslistener (LISTEN)
3.6 配置Nginx支持PHP程序請求訪問
3.6.1 修改Nginx配置文件
(1)查看nginx當前的配置,命令如下:
[root@localhost etc]# cd /usr/local/nginx/conf/ [root@localhost conf]# cp nginx.conf nginx.conf.02 [root@localhost conf]# cat nginx.conf worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/mail.conf; include extra/status.conf; include extra/blog.conf; }
(2)PHP解析,這里以blog為例講解,內容如下:
[root@localhost conf]# cat extra/blog.conf server { listen 80; server_name blog.yunjisuan.com; location / { root /var/www/html/blogcom; index index.html index.htm; } }
最終blog虛擬機的完整配置如下:
[root@localhost conf]# cat extra/blog.conf server { listen 80; server_name blog.yunjisuan.com; location / { root /var/www/html/blogcom; index index.html index.htm; } location ~ .*\.(php|php5)?$ { root /var/www/html/blogcom; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }
3.6.2 檢查並啟動Nginx
可通過如下命令檢查Nginx配置文件的語法:
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.10.2//conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.10.2//conf/nginx.conf test is successful [root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
此步在生產環境很關鍵,如不提前檢查語法,重啟后發現語法錯誤會導致Nginx無法提供服務,,給用戶訪問體驗帶來不好的影響。
3.6.3 測試LNMP環境生效情況
(1)測試PHP解析請求是否OK
1)進入指定的默認站點目錄后,編輯index.php,添加如下內容:
[root@localhost conf]# cd /var/www/html/blogcom/ [root@localhost blogcom]# echo "<?php phpinfo(); ?>" >test_info.php [root@localhost blogcom]# cat test_info.php <?php phpinfo(); ?>
以上代碼為顯示PHP配置信息的簡單PHP文件代碼
注意:
對於初學者來說,以上內容最好手工錄入而不要拷貝,否則可能會導致意外結果。
2)調整Windows下的host解析(192.168.0.121為當前的機器IP),命令如下:
192.168.0.121 www.yunjisuan.com mail.yunjisuan.com yunjisuan.com blog.yunjisuan.com
3)打開瀏覽器,輸入http://blog.yunjisuan.com/test_info.php 即可打開如下圖所示界面:
出現上述界面,表示Nginx配合PHP解析已經正常。
(2)針對Nginx請求訪問PHP,然后對PHP連接MySQL的情況進行測試
編輯test_mysql.php,加入如下內容:
[root@localhost blogcom]# cat test_mysql.php <?php //$link_id=mysql_connect('主機名','用戶','密碼'); $link_id=mysql_connect('localhost','root','123123'); if($link_id){ echo "mysql successful by Mr.chen !"; }else{ echo mysql_error(); } ?>
測試結果如下:
至此,LNMP的組合已基本搭建完畢。當然,我們還沒有做相關優化,因此,我們需要將虛擬機保存好。留待以后之用
四, 部署一個blog程序服務
4.1 開源博客程序WordPress介紹
WordPress 是一套利用PHP語言和MySQL數據庫開發的開源免費的blog(博客,網站)程序,用戶可以在支持PHP環境和MySQL數據庫的服務器上建立blog站點。它的功能非常強大,擁有眾多插件,易於擴充功能。其安裝和使用也都非常方便。目前WordPress已經成為搭建blog平台的主流,很多發布平台都是根據WordPress二次開發的,如果你也想像他們一樣擁有自己的blog,可購買網上的域名及空間,然后搭建LNMP環境,部署WordPress程序后就可以輕松成就自己的夢想了。
注意:
WordPress是單用戶個人博客,與blog.51cto.com的多用戶博客是有區別的。
4.2 WordPress 博客程序的搭建准備
(1)MySQL數據庫配置准備
1)登陸MySQL數據庫,操作如下:
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server 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 its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
2)創建一個專用的數據庫WordPress,用於存放blog數據,操作如下:
mysql> create database wordpress; #創建一個數據庫,名字為wordpress Query OK, 1 row affected (0.00 sec) mysql> show databases like 'wordpress'; #查看 +----------------------+ | Database (wordpress) | +----------------------+ | wordpress | +----------------------+ 1 row in set (0.00 sec) mysql>
3)創建一個專用的WordPress blog管理用戶,命令如下:
mysql> grant all on wordpress.* to wordpress@'localhost' identified by '123123'; #localhost為客戶端地址 Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; #刷新權限,使得創建用戶生效 Query OK, 0 rows affected (0.00 sec) mysql> show grants for wordpress@'localhost'; #查看用戶對應權限 +------------------------------------------------------------------------------------------------------------------+ | Grants for wordpress@localhost | +------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'wordpress'@'localhost' IDENTIFIED BY PASSWORD '*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1' | | GRANT ALL PRIVILEGES ON `wordpress`.* TO 'wordpress'@'localhost' | +------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> select user,host from mysql.user; #查看數據庫里創建的wordpress用戶 +-----------+-----------+ | user | host | +-----------+-----------+ | root | 127.0.0.1 | | root | localhost | | wordpress | localhost | #只允許本機通過wordpress用戶訪問數據庫 +-----------+-----------+ 3 rows in set (0.00 sec) mysql> quit Bye
(2)Nginx及PHP環境配置准備
1)選擇之前配置好的支持LNMP的blog域名對應的虛擬主機,命令如下:
[root@localhost extra]# cat blog.conf server { listen 80; server_name blog.yunjisuan.com; location / { root /var/www/html/blogcom; index index.php index.html index.htm; #補充一個首頁文件index.php } location ~ .*\.(php|php5)?$ { root /var/www/html/blogcom; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } } [root@localhost extra]# /usr/local/nginx/sbin/nginx -s reload
2)獲取WordPress博客程序,並放置到blog域名對應虛擬主機的站點目錄下,即/var/www/html/blogcom,操作命令如下:
[root@localhost blogcom]# ls #瀏覽www.wordpress.org下載博客程序 index.html test_info.php test_mysql.php wordpress-4.7.4-zh_CN.tar.gz [root@localhost blogcom]# tar xf wordpress-4.7.4-zh_CN.tar.gz #解壓 [root@localhost blogcom]# ls index.html test_info.php test_mysql.php wordpress wordpress-4.7.4-zh_CN.tar.gz [root@localhost blogcom]# rm -f index.html test_info.php test_mysql.php #刪除無用文件 [root@localhost blogcom]# ls wordpress wordpress-4.7.4-zh_CN.tar.gz [root@localhost blogcom]# mv wordpress/* . #把目錄里的內容移動到blogcom根目錄下 [root@localhost blogcom]# /bin/mv wordpress-4.7.4-zh_CN.tar.gz /root/ #移走源程序 [root@localhost blogcom]# ls -l #完整的blog程序內容 total 192 -rw-r--r--. 1 nobody 65534 418 Sep 24 2013 index.php -rw-r--r--. 1 nobody 65534 19935 Jan 2 2017 license.txt -rw-r--r--. 1 nobody 65534 6956 Apr 23 09:24 readme.html drwxr-xr-x. 2 nobody 65534 4096 Jul 14 16:04 wordpress -rw-r--r--. 1 nobody 65534 5447 Sep 27 2016 wp-activate.php drwxr-xr-x. 9 nobody 65534 4096 Apr 23 09:24 wp-admin -rw-r--r--. 1 nobody 65534 364 Dec 19 2015 wp-blog-header.php -rw-r--r--. 1 nobody 65534 1627 Aug 29 2016 wp-comments-post.php -rw-r--r--. 1 nobody 65534 2930 Apr 23 09:24 wp-config-sample.php drwxr-xr-x. 5 nobody 65534 4096 Apr 23 09:24 wp-content -rw-r--r--. 1 nobody 65534 3286 May 24 2015 wp-cron.php drwxr-xr-x. 18 nobody 65534 12288 Apr 23 09:24 wp-includes -rw-r--r--. 1 nobody 65534 2422 Nov 20 2016 wp-links-opml.php -rw-r--r--. 1 nobody 65534 3301 Oct 24 2016 wp-load.php -rw-r--r--. 1 nobody 65534 33939 Nov 20 2016 wp-login.php -rw-r--r--. 1 nobody 65534 8048 Jan 11 2017 wp-mail.php -rw-r--r--. 1 nobody 65534 16255 Apr 6 14:23 wp-settings.php -rw-r--r--. 1 nobody 65534 29896 Oct 19 2016 wp-signup.php -rw-r--r--. 1 nobody 65534 4513 Oct 14 2016 wp-trackback.php -rw-r--r--. 1 nobody 65534 3065 Aug 31 2016 xmlrpc.php root@localhost blogcom]# chown -R www.www ../blogcom/ #授權用戶訪問 [root@localhost blogcom]# ls -l #最終博客目錄和權限 total 192 -rw-r--r--. 1 www www 418 Sep 24 2013 index.php -rw-r--r--. 1 www www 19935 Jan 2 2017 license.txt -rw-r--r--. 1 www www 6956 Apr 23 09:24 readme.html drwxr-xr-x. 2 www www 4096 Jul 14 16:04 wordpress -rw-r--r--. 1 www www 5447 Sep 27 2016 wp-activate.php drwxr-xr-x. 9 www www 4096 Apr 23 09:24 wp-admin -rw-r--r--. 1 www www 364 Dec 19 2015 wp-blog-header.php -rw-r--r--. 1 www www 1627 Aug 29 2016 wp-comments-post.php -rw-r--r--. 1 www www 2930 Apr 23 09:24 wp-config-sample.php drwxr-xr-x. 5 www www 4096 Apr 23 09:24 wp-content -rw-r--r--. 1 www www 3286 May 24 2015 wp-cron.php drwxr-xr-x. 18 www www 12288 Apr 23 09:24 wp-includes -rw-r--r--. 1 www www 2422 Nov 20 2016 wp-links-opml.php -rw-r--r--. 1 www www 3301 Oct 24 2016 wp-load.php -rw-r--r--. 1 www www 33939 Nov 20 2016 wp-login.php -rw-r--r--. 1 www www 8048 Jan 11 2017 wp-mail.php -rw-r--r--. 1 www www 16255 Apr 6 14:23 wp-settings.php -rw-r--r--. 1 www www 29896 Oct 19 2016 wp-signup.php -rw-r--r--. 1 www www 4513 Oct 14 2016 wp-trackback.php -rw-r--r--. 1 www www 3065 Aug 31 2016 xmlrpc.php
4.3 開始安裝blog博客程序
很多開源程序都支持瀏覽器傻瓜式的界面安裝,此處也用這種方法。
1)打開瀏覽器輸入blog.yunjisuan.com(提前做好hosts或DNS解析),回車后,出現下圖:
2)仔細閱讀頁面的文字信息后,單擊“現在就開始”按鈕繼續,然后在出現的頁面表單上填寫相應的內容,如下圖所示:
3)在頁面表單里填好內容后,單擊結尾的“提交”按鈕繼續,得到下圖:
4)出現上圖就表示可以安裝了,單擊“進行安裝”按鈕繼續,進入下圖:
5)根據界面提示設置blog站點的信息后,單擊“安裝WordPress”按鈕繼續。
出現下圖所示的信息就表明已經成功安裝了WordPress博客。
4.4 博客的簡單使用
(1)后台登錄,如下圖:
其他功能同學們自己玩
4.5 實現WordPress博客程序URL靜態化
實現此功能時,首先要在WordPress后台依次單擊設置--->固定鏈接--->自定義結構,然后輸入下面的代碼,並保存更改。
/archives/%post_id%.html #說明:%post_id%是數據庫對應博文內容的唯一ID,例如423
接着,在Nginx配置文件的server容器中添加下面的代碼:
[root@localhost extra]# cat blog.conf server { listen 80; server_name blog.yunjisuan.com; root /var/www/html/blogcom; location / { index index.php index.html index.htm; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } }
最后檢查語法並重新加載Nginx服務,操作如下:
[root@localhost extra]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.10.2//conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.10.2//conf/nginx.conf test is successful [root@localhost extra]# /usr/local/nginx/sbin/nginx -s reload
現在可以通過瀏覽器訪問了,如下圖所示:
五, 本章重點回顧
- LNMP的組合中各組件工作調度邏輯關系。
- Nginx與PHP通過FastCGI模式通信的原理。
- LNMP環境的企業級搭建。
- WordPress博客程序的安裝搭建與URL靜態化