Nginx工做原理php
這裏須要結合Apache的工做,對PHP文件處理過程的區別html
1:Nginx是經過php-fpm這個服務來處理php文件mysql
2:Apache是經過libphp5.so這個模塊來處理php文件nginx
Apache的libphp5.so隨着apache服務器一塊兒運行,而Nginx和php-fpm是各自獨立運行,因此在運行過程當中,Nginx和php-fpm都須要分別啓動!c++
修改Nginx配置文件,啓動nginx服務,修改php配置文件,啓動php-fpm服務正則表達式
部署LNMP架構須要安裝依賴包算法
yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsofsql
1、安裝Nginx數據庫
下載源碼包:apache
安裝epel擴展yum源
[root@xuegod63 ~]# yum –y install epel-release
[root@xuegod63 ~]# yum clean all
[root@xuegod63 ~]# yum list
所需依賴包:
[root@xuegod63 ~]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
Zlib:Nginx提供gzip模塊,須要zlib的支持
Openssl:Nginx提供SSL的功能
建立Nginx運行用戶
[root@xuegod63 ~]# useradd -M -s /sbin/nologin nginx
下載PCRE庫https:#ftp.pcre.org/pub/pcre/
上傳源碼包
[root@xuegod63 ~]# unzip pcre-8.38.zip -d /usr/local/src
注:解壓便可,不用安裝,Nginx安裝時指定pcre的解壓路徑便可
[root@xuegod63 ~]# tar zxf nginx-1.10.3.tar.gz -C /usr/local/src;cd /usr/local/src/nginx-1.10.3
[root@xuegod63 nginx-1.10.3]# ./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-http_ssl_module --with-pcre=/usr/local/src/pcre-8.38 --user=nginx --group=nginx
注:
--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-http_ssl_module #nginx並支持ssl
--with-pcre=/usr/local/src/pcre-8.37 #須要注意,這裏指的是源碼,用#./configure --help |grep pcre查看幫助
[root@xuegod63 nginx-1.10.3]# make -j 4 && make install
[root@xuegod63 nginx-1.10.3]# ll /usr/local/nginx/
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 conf #Nginx相關配置文件
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 html #網站根目錄
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 logs #日誌文件
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 sbin #Nginx啓動腳本
配置Nginx支持php文件
[root@xuegod63 nginx-1.10.3]# vim /usr/local/nginx/conf/nginx.conf
修改用戶爲nginx:
2 #user nobody;
3 user nginx nginx;
啓用PHP支持
第66行始 修改成:
66 location ~ \.php$ {
67 root html;
68 fastcgi_pass 127.0.0.1:9000;
69 fastcgi_index index.php;
70 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
71 include fastcgi_params;
72 }
啓動Nginx服務
[root@xuegod63 nginx-1.8.0]# /usr/local/nginx/sbin/nginx
優化Nginx啓動命令執行路徑
[root@xuegod63 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
生成服務啓動腳本
[root@xuegod63 nginx-1.10.3]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ] ; then continue ; fi
$0 start
;;
reload)
kill -1 $(cat $PIDF)
;;
*)
echo "Userage: $0 { start | stop | restart | reload }"
exit 1
esac
exit 0
配置服務開機自動啓動
[root@xuegod63 ~]# chmod +x /etc/init.d/nginx
[root@xuegod63 ~]# chkconfig --add nginx
[root@xuegod63 ~]# chkconfig nginx on
先關閉iptables 再瀏覽器訪問驗證:
擴展:Nginx維護命令
[root@xuegod63 ~]# nginx -t #檢查配置文件是否有語法錯誤
nginx: the configuration file /server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /server/nginx/conf/nginx.conf test is successful
[root@xuegod63 ~]# nginx -V #查看Nginx版本和配置參數
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
configure arguments: --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 --user=nginx --group=nginx
注:從新編譯時,必定要查看之前的編譯配置,只需在原有配置參數後添加新的參數便可
[root@xuegod63 ~]# nginx -s reload #重載Nginx配置文件
2、安裝MySQL
刪除系統自帶mysql
[root@xuegod63 ~]# yum -y remove mysql
解決依賴
[root@xuegod63 ~]# yum -y install gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel* make cmake
安裝yum 提示報錯:出現GPG key retrieval failed 的解決:
把yum源文件 驗證key關閉,即改成gpgcheck=0
添加用戶和組
[root@xuegod63 ~]# groupadd mysql
[root@xuegod63 ~]# useradd -M -s /sbin/nologin -r -g mysql mysql
建立安裝目錄和數據存放目錄(生產環境建議增長一塊硬盤掛載做mysql目錄)
[root@xuegod63 ~]# mkdir -p /usr/local/mysql/data
上傳源碼包到服務器LNMP目錄下或者直接wget進行下載源碼包
(rz命令上傳或FTP上傳)
解壓源碼包後進入目錄
[root@xuegod63 LNMP]# tar zxf mysql-5.6.26.tar.gz -C /usr/local/src/ ; cd /usr/local/src/mysql-5.6.26
編譯
[root@xuegod63 ~]#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
編譯的參數能夠參考http:#dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html
CMAKE_INSTALL_PREFIX:指定MySQL程序的安裝目錄,默認/usr/local/mysql
DEFAULT_CHARSET:指定服務器默認字符集,默認latin1
DEFAULT_COLLATION:指定服務器默認的校對規則,默認latin1_general_ci
ENABLED_LOCAL_INFILE:指定是否容許本地執行LOAD DATA INFILE,默認OFF
WITH_COMMENT:指定編譯備註信息
WITH_xxx_STORAGE_ENGINE:指定靜態編譯到mysql的存儲引擎,MyISAM,MERGE,MEMBER以及CSV四種引擎默認即被編譯至服務器,不須要特別指定。
WITHOUT_xxx_STORAGE_ENGINE:指定不編譯的存儲引擎
SYSCONFDIR:初始化參數文件目錄
MYSQL_DATADIR:數據文件目錄
MYSQL_TCP_PORT:服務端口號,默認3306
MYSQL_UNIX_ADDR:socket文件路徑,默認/tmp/mysql.sock
MYSQL-USER:運行 mysql的用戶
開始編譯和安裝
[root@xuegod63 mysql-5.6.26]# make -j 4 && make install
配置 mysql
[root@xuegod63 ~]# chown -R mysql:mysql /usr/local/mysql/ 更改屬主
[root@xuegod63 ~]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf #覆蓋原配置文件,並更改my.cnf數據目錄位置
[root@xuegod63 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
生成服務啓動腳本
[root@xuegod63 ~]# chmod 777 /etc/init.d/mysqld
[root@xuegod63 ~]# chkconfig --add mysqld
[root@xuegod63 ~]# chkconfig mysqld on
[root@xuegod63 ~]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
初始化數據庫(重要務必執行)
[root@xuegod63 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
啓動服務
[root@xuegod63 ~]#/etc/init.d/mysqld start
設置環境變量
[root@xuegod63 ~]#ln -s /usr/local/mysql/bin/* /usr/sbin/ 讓系統直接調用
3、安裝PHP
在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官方發出來的通知:
解決依賴:yum安裝解決
yum -y install php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel
解壓PHP包並配置:
[root@xuegod63~]# tar zxf php-7.0.5.tar.gz -C /usr/local/src/;cd /usr/local/src/php-7.0.5
[root@xuegod63 php-7.0.5]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/ --enable-fpm --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --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-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
參數選項可參考http:#php.net/manual/zh/configure.about.php官方中文手冊
--with-config-file-path #設置 php.ini 的搜索路徑。默認爲 PREFIX/lib
--with-mysql #mysql安裝目錄,對mysql的支持 7.0版本沒有此參數
--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算法擴展
編譯並安裝
[root@xuegod63 php-7.0.5]# make -j 4 && make install
修改fpm配置php-fpm.conf.default文件名稱
[root@xuegod63 php-7.0.5]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
修改運行用戶和組
[root@xuegod63 php-7.0.5]#vim !$
user = nginx
group = nginx
複製php.ini配置文件
[root@xuegod63 php-7.0.5]# cp /usr/local/src/php-7.0.5/php.ini-production /usr/local/php/php.ini
複製php-fpm啓動腳本到init.d
[root@xuegod63 php-7.0.5]# cp /usr/local/src/php-7.0.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
賦予執行權限
[root@xuegod63 php-7.0.5]# chmod +x /etc/init.d/php-fpm
添加爲啓動項
[root@xuegod63 php-7.0.5]# chkconfig --add php-fpm
設置開機啓動
[root@xuegod63 php-7.0.5]# chkconfig php-fpm on
啓動服務
[root@xuegod63 php-7.0.5]# /etc/init.d/php-fpm start
Starting php-fpm done
查看端口監聽狀態
[root@xuegod63 php-7.0.5]# netstat -antpu | grep php-fpm
驗證:
[root@xuegod63 ~]# vim /usr/local/nginx/html/a.php
<?php
phpinfo();
?>