一、nginx概述
LNMP=Linux Nginx Mysql PHP
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 伺服器。Nginx 是由 Igor Sysoev 為俄羅斯通路量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0釋出于2004年10月4日。其将源代碼以類BSD許可證的形式釋出,因它的穩定性、豐富的功能集、示例配置檔案和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4釋出。
Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,并在一個BSD-like 協定下發行。由俄羅斯的程式設計師Igor Sysoev所開發,供俄國大型的入口網站及搜尋引擎Rambler(俄文:Рамблер)使用。其特點是占有記憶體少,并發能力強,事實上nginx的并發能力确實在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度BWS、新浪、網易、騰訊等
Nginx官方網站
RamBler:http://www.rambler.ru/ Nginx:http://nginx.org/

在Nginx中,我們使用的是php-fpm來對php頁面解析,PHP-FPM其實是PHP源代碼的一個更新檔,指在将FastCGI程序管理整合進PHP包中。必須将它patch到你的PHP源代碼中,再編譯安裝PHP後才可以使用
從PHP5.3.3開始,PHP中直接整合了PHP-FPM,是以從PHP5.3.3版本以後,不需要下載下傳PHP-FPM更新檔包了,下面是PHP-FPM官方發出來的通知:
http://php-fpm.org/download
二、Nginx的工作原理
Nginx 本身隻支援靜态頁面的處理,當用戶端通路php頁面的時候,nginx會将php轉到php-fpm也處理,
php-fpm服務會把php頁面解析成html檔案給nginx處理,nginx傳回給用戶端處理
這裡需要結合Apache的工作,對PHP檔案處理過程的差別
1:Nginx是通過php-fpm這個服務來處理php檔案
2:Apache是通過libphp5.so這個子產品來處理php檔案
Apache:
Nginx:
Apache的libphp5.so随着apache伺服器一起運作,而Nginx和php-fpm是各自獨立運作,是以在服務的運作過程中,Nginx和php-fpm都需要分别啟動!
修改Nginx配置檔案,啟動nginx服務,修改php配置檔案,啟動php-fpm服務。
三、安裝環境
系統環境:CentOS release 6.9 64bit
軟體名稱:libmcrypt-2.5.8、mysql-5.6.26、nginx-1.9.4、pcre-8.37、php-5.6.13
軟體安裝位置:/usr/local/
資料存放位置:/usr/local/nginx/html、/usr/local/mysql/data
日志存放位置:/usr/local/nginx/logs、/usr/local/mysql/logs
四、安裝lnmp
1、編譯安裝nginx
1)安裝開發工具包
yum -y groupinstall "Development Tools" "Development Libraries"
2)安裝依賴包
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* pcre-devel
3)在home建立lnmp檔案夾,将對應軟體包上傳到這個檔案夾
[root@centos-6 lnmp]# ll
total 50292
-rw-r--r-- 1 root root 875425 Jun 10 07:10 libmcrypt-2.5.8.tar.bz2
-rw-r--r-- 1 root root 33278710 Jun 10 07:10 mysql-5.6.26.tar.gz
-rw-r--r-- 1 root root 832104 Jun 10 07:10 nginx-1.8.0.tar.gz
-rw-r--r-- 1 root root 866423 Jun 10 07:11 nginx-1.9.4.tar.gz
-rw-r--r-- 1 root root 1557603 Jun 10 07:10 pcre-8.37.tar.bz2
-rw-r--r-- 1 root root 14077633 Jun 10 07:10 php-5.6.13.tar.bz2
[root@centos-6 lnmp]#
4)解壓pcre軟體包
解壓此安裝包即可,不需要安裝,Nginx需要指定pcre的源碼不是安裝後的路徑,此包的功能是支援位址重寫rewrite功能 pcre的依賴可以yum安裝pcre和pcre-devel解決!
tar jxvf pcre-8.37.tar.bz2 -C /usr/local/src/
5)編譯安裝nginx
[root@centos-6 lnmp]# tar zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[root@centos-6 lnmp]# cd /usr/local/src/nginx-1.9.4/[root@centos-6 nginx-1.9.4]# ./configure --prefix=/usr/local/nginx --with-http_dav_module
--with-http_stub_status_module --with-http_addition_module
--with-http_sub_module --with-http_flv_module --with-http_mp4_module
--with-pcre=/usr/local/src/pcre-8.37
6)功能子產品說明:
--with-http_dav_module #啟用支援(增加PUT,DELETE,MKCOL:建立集合,COPY和MOVE方法)
預設關閉,需要編譯開啟
--with-http_stub_status_module #啟用支援(擷取Nginx上次啟動以來的工作狀态)
--with-http_addition_module #啟用支援(作為一個輸出過濾器,支援不完全緩沖,分部分相應請求)
--with-http_sub_module #啟用支援(允許一些其他文本替換Nginx相應中的一些文本)
--with-http_flv_module #啟用支援(提供支援flv視訊檔案支援)
--with-http_mp4_module #啟用支援(提供支援mp4視訊檔案支援,提供僞流媒體服務端支援)
--with-pcre=/usr/local/src/pcre-8.37 #需要注意,這裡指的是源碼,用#./configure --help |grep pcre檢視幫助
7)采用多線程編譯加快速度
[root@centos-6 nginx-1.9.4]# make -j 4 && make install
8)建立沒家目錄及登入權限的nginx運作帳号
[root@centos-6 lnmp]# useradd -M -s /sbin/nologin nginx
9)配置nginx支援php及修改運作使用者
[root@centos-6 nginx-1.9.4]#vim /usr/local/nginx/conf/nginx.conf
#user nobody;
user nginx nginx; #添加此行
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} #找到上面這段内容,将這段内容複制,去掉#且修改為如下
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #将scripts修改為nginx的html,即Nginx頁面目錄,因為要處理的php檔案也在這個目錄下
include fastcgi_params;
}
10)啟動nginx并檢視狀态是否啟動成功
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx
[root@centos-6 nginx-1.9.4]# ps -aux | grep nginx
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root 7715 0.0 0.0 21756 840 ? Ss 07:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 7716 0.0 0.0 22108 1412 ? S 07:38 0:00 nginx: worker process
root 7718 0.0 0.0 103328 876 pts/2 S+ 07:38 0:00 grep nginx
[root@centos-6 nginx-1.9.4]# netstat -antup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7715/nginx
[root@centos-6 nginx-1.9.4]#
11)nginx維護指令
[root@centos-6 nginx-1.9.4]# /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@centos-6 nginx-1.9.4]#
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -s reload //重新加載配置文而建(平滑重新開機)平滑重新開機(保持了C-S連結,不斷開,伺服器隻是重新加載了配置檔案,沒有開啟和關閉的伺服器的一個動作)
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -s stop //停止Nginx,注意:啟動沒有任何參數
[root@centos-6 nginx-1.9.4]# echo "/usr/local/nginx/sbin/nginx &" >> /etc/rc.local
12)檢視nginx是否安裝成功
2、編譯安裝mysql
1)清理已安裝的mysql
[root@centos-6 home]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
[root@centos-6 home]# yum remove mysql-libs
[root@centos-6 home]#
[root@centos-6 home]# yum install -y cmake ncurses-devel
3)編譯安裝mysql
[root@centos-6 lnmp]# tar xf mysql-5.6.26.tar.gz -C /usr/local/src/
3)建立運作mysql的帳号
[root@centos-6 mysql-5.6.26]# useradd -M -s /sbin/nologin mysql
4)配置編譯參數
[root@centos-6 mysql-5.6.26]#cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1\
-DWITH_INNOBASE_STORAGE_ENGINE=1\
-DWITH_MEMORY_STORAGE_ENGINE=1\
-DWITH_READLINE=1\
-DENABLED_LOCAL_INFILE=1\
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL-USER=mysql
5)編譯參數說明
DCMAKE_INSTALL_PREFIX #制定mysql的安裝根目錄,目錄在安裝的時候會自動建立,這個值也可以在伺服器啟動時,用--basedir來設定
DMYSQL_UNIX_ADDR #伺服器與本地用戶端進行通信的Unix套接字檔案,必須是絕對路徑,預設位置/tmp/mysql.sock,可以在伺服器啟動時,用--socket改變
DDEFAULT_CHARSET #mysql預設使用的字元集,不指定将預設使用Latin1西歐字元集
DDEFAULT_COLLATION #預設字元校對
DWITH_EXTRA_CHARSETS #制定mysql拓展字元集,預設值也是all支援所有的字元集
DWITH_MYISAM_STORAGE_ENGINE #靜态編譯MYISAM,INNOBASE,MEMORY存儲引擎到MYSQL服務
器,這樣MYSQL就支援這三種存儲引擎
DWITH_INNOBASE_STORAGE_ENGINE
DWITH_MEMORY_STORAGE_ENGINE
DWITH_READLINE #支援readline庫
DENABLED_LOCAL_INFILE #允許本地倒入資料,啟用加載本地資料
DMYSQL_DATADIR #mysql資料庫存放路徑
DMYSQL-USER #運作mysql的使用者
這些編譯參數的幫助尋找方法:
http://www.mysql.com→→Documentation→→選擇對應的版本(5.6)→→HTML Online→→View→→Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options→→
http://dev.mysql.com/doc/refman/ ... ration-options.html
6)編譯安裝
[root@centos-6 mysql-5.6.26]#make -j 4 ; make install
//以4個程序來加快編譯速度,但不要超過CPU核心數,編譯時間較長需耐心等待
7)配置mysql
[root@centos-6 mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/ //屬主屬組更改
[root@centos-6 mysql-5.6.26]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf //覆寫原配置檔案
[root@centos-6 mysql-5.6.26]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld //複制啟動腳本
[root@centos-6 mysql-5.6.26]# vim /etc/init.d/mysqld //更改啟動腳本中指定mysql位置
basedir=
datadir=
#修改為
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
[root@centos-6 mysql-5.6.26]# chkconfig mysqld on //設定開機啟動
8)初始化資料庫
[root@centos-6 mysql-5.6.26]#/usr/local/mysql/scripts/mysql_install_db \
--defaults-file=/etc/my.cnf \
--basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data/ \
--user=mysql
檢視初始化是否成功
[root@centos-6 mysql-5.6.26]# ls /usr/local/mysql/data/
ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
[root@centos-6 mysql-5.6.26]#
[root@centos-6 mysql-5.6.26]# ln -s /usr/local/mysql/bin/* /bin/ ///這個裡面是部分指令,讓系統直接調用
[root@centos-6 mysql-5.6.26]# /etc/init.d/mysqld restart //啟動資料庫
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!
[root@centos-6 mysql-5.6.26]# mysql_secure_installation ////初始安全設定,設定root密碼,急用root遠端通路,删除test資料庫等;
9)測試登陸是否正常
[root@centos-6 mysql-5.6.26]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.26 Source distribution
Copyright (c) 2000, 2015, 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>
3、編譯安裝php
1)解決依賴關系
yum -y install php-pear
//pear按照一定的分類來管理pear應用代碼庫,你
的pear代碼可以組織到其中适當的目錄中,其他人可以友善的檢索并分享到你的成果;pear不僅僅是一個代碼倉庫,它同時也是一個标準,使用這個标準來書寫你的php代碼,将會增強你的程式的可讀性,複用性,減少出錯的幾率;Pear通過兩個類為你搭建了一個架構,實作了諸如析構函數,錯誤捕獲功能,你通過繼承就可以使用這些功能
PHP添加libmcrypt拓展
libmcrypt加密算法擴充庫,支援DES, 3DES,RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法
官方網站:http://mcrypt.hellug.gr/index.html 或者 http://mcrypt.sourceforge.net/
在頁面上點選下圖連接配接,可以下載下傳最新的2.5.8版本(頁面上有個Browse All Files→→Libmcrypt)
2)安裝加密算法擴充庫
[root@centos-6 lnmp]# tar xf libmcrypt-2.5.8.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/libmcrypt-2.5.8/
[root@centos-6 libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt ; make -j 4 ; make install
3)除開上面的依賴解決之外,還需要安裝圖檔,xml,字型支援基本庫,使用yum去安裝,安裝的時候,這些軟體包自身也有依賴!
[root@centos-6 libmcrypt-2.5.8]# yum install -y libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel libzip libzip-devel
4)需要添加到庫檔案路徑
[root@centos-6 libmcrypt-2.5.8]# vim /etc/ld.so.conf
添加如下内容
include ld.so.conf.d/*.conf
/usr/local/libmcrypt/lib #此行添加
/usr/local/mysql/lib #此行添加
[root@centos-6 libmcrypt-2.5.8]# ldconfig
[root@centos-6 libmcrypt-2.5.8]# echo 'ldconfig' >> /etc/rc.local
5)加壓php
[root@centos-6 lnmp]# tar xf php-5.6.13.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/php-5.6.13
[root@centos-6 php-5.6.13]#
6)配置php編譯參數
[root@centos-6 php-5.6.13]#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-mysqlnd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --with-mcrypt=/usr/local/libmcrypt --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd
7)編譯參數說明
--with-config-file-path #設定 php.ini 的搜尋路徑。預設為 PREFIX/lib
--with-mysql #mysql安裝目錄,對mysql的支援
--with-mysqli #mysqli擴充技術不僅可以調用MySQL的存儲過程、處理MySQL事務,而且還可以使通路資料庫工作變得更加穩定。是一個資料庫驅動
--with-iconv-dir #種字元集間的轉換
--with-freetype-dir #打開對freetype字型庫的支援
--with-jpeg-dir #打開對jpeg圖檔的支援
--with-png-dir #打開對png圖檔的支援
--with-zlib #打開zlib庫的支援,實作GZIP壓縮輸出
--with-libxml-dir=/usr #打開libxml2庫的支援,libxml是一個用來解析XML文檔的函數庫
--enable-xml #支援xml文檔
--disable-rpath #關閉額外的運作庫檔案
--enable-bcmath #打開圖檔大小調整,用到zabbix監控的時候用到了這個子產品
--enable-shmop #shmop共享記憶體操作函數,可以與c/c++通訊
--enable-sysvsem #加上上面shmop,這樣就使得你的PHP系統可以處理相關的IPC函數(活動在核心級别)。
--enable-inline-optimization #優化線程
--with-curl #打開curl浏覽工具的支援
--with-curlwrappers #運用curl工具打開url流 ,新版PHP5.6已棄用
--enable-mbregex #支援多位元組正規表達式
--enable-fpm #CGI方式安裝的啟動程式,PHP-FPM服務
--enable-mbstring #多位元組,字元串的支援
--with-gd #打開gd庫的支援,是php處理圖形的擴充庫,GD庫提供了一系列用來處理圖檔的API,使用GD庫可以處理圖檔,或者生成圖檔。
--enable-gd-native-ttf #支援TrueType字元串函數庫
--with-openssl #打開ssl支援
--with-mhash #支援mhash算法擴充
--enable-pcntl #freeTDS需要用到的,pcntl擴充可以支援php的多線程操作
--enable-sockets #打開 sockets 支援
--with-xmlrpc #打開xml-rpc的c語言
--enable-zip #打開對zip的支援
--enable-soap #擴充庫通過soap協定實作了客服端與伺服器端的資料互動操作
--with-mcrypt #mcrypt算法擴充
--with-zlib-dir=/usr/local/libzip ##指定zip庫路徑
8)編譯安裝
[root@centos-6 php-5.6.13]# make -j 3 && make install
9)配置php和php-fpm
[root@centos-6 php-5.6.13]# cp /usr/local/src/php-5.6.13/php.ini-production /usr/local/php/php.ini //替換php配置檔案
[root@centos-6 php-5.6.13]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf //替換php-fpm配置檔案
[root@centos-6 php-5.6.13]# chown -R nginx:nginx /usr/local/php/etc/php-fpm.conf //修改php-fpm.conf檔案權限
[root@centos-6 nginx]# vim /usr/local/php/etc/php-fpm.conf //修改php-fpm運作帳号為nginx
将
user = nobody
group = nobody
改為