nginx 環境搭建使用之入門


1、http://nginx.org/下載最新的nginx

現在最新的版本是nginx-1.9.1   下載.tar.gz包 ,解壓。

timeless@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/src/nginx-1.9.1$ ./configure

configure 過程中可能會遇到錯誤

./configure: error: the HTTP rewrite module requires the PCRE library.   //大體意思是重寫模塊需要pcre模塊  
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解決辦法

http://www.pcre.org/       編譯安裝便可 

執行操作

./configure  &&make &&make install

到現在為止,已經安裝nginx ,接下來我們要把 nginx 與php 結合起來

2、首先介紹知識:

nginx本身不能處理PHP,它只是個web服務器,當接收到請求后,如果是php請求,則發給php解釋器處理,並把結果返回給客戶端。

nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回被nginx

本文以php-fpm為例介紹如何使nginx支持PHP

什么是PHP-FPM

PHP-FPM是一個PHP FastCGI管理器,是只用於PHP的,可以在 http://php-fpm.org/download下載得到.

PHP-FPM其實是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP后才可以使用。

新版PHP已經集成php-fpm了,不再是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,可以有效控制內存和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優點,所以被PHP官方收錄了。

就是FastCGI Process Manager,是一種可選的PHP FastGCI執行模式,有一點很有特點的應用,尤其是一個繁忙的網站中:

(1) 可適應的進行再生(NEW!)

(2) 基本的統計功能(Apache's mod_status)

(3) 高級進程管理功能,能夠優雅的停止/開始

(4) 能夠使用不同的工作用戶和不同的php.ini

(5) 輸入,輸出日志記錄...

 

新的版本的php在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM,其它參數都是配置php的,具體選項含義可以查看這里

下面是我php 編譯安裝時候的選項:

./configure  --prefix=/usr/local/php/ --with-config-file-path=/etc/php5/cli/  --with-config-file-scan-dir=/etc/php5/mods-available/  --with-apxs2=/usr/local/apache243/bin/apxs  --with-mysql   --with-libxml-dir=/usr/local/libxml2/ --with-png-dir=/usr/local/libpng/ --with-jpeg-dir=/usr/local/jpeg8/ --with-freetype-dir=/usr/local/freetype/  --with-zlib-dir=/usr/local/zlib/ --with-mcrypt=/usr/local/libmcrypt/ --with-mysqli  --enable-soap --enable-mbstring=all --enable-sockets --enable-memcache --with-pdo-mysql --with-pdo-pgsql --with-pgsql --with-curl -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-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex  --enable-mbstring  --enable-fpm

3、php編譯安裝完成,下面是對php-fpm運行用戶進行設置

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf

修改
user = www-data
group = www-data

如果www-data用戶不存在,那么先添加www-data用戶
groupadd www-data
useradd -g www-data www-data

 

4、配置 nginx.conf文件

其中server段增加如下配置,注意標紅內容配置,否則會出現No input file specified.錯誤

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;  //端口不可以自己更改
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5、創建測試文件

創建php文件

在/usr/local/nginx/html下創建index.php文件,輸入如下內容

<?php echo phpinfo(); ?>

6、出現問題:

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check theerror log for details.

Faithfully yours, nginx.

先在一個目錄下運行下test.html和test.php,結果html可以運行,php無法運行。

證實是php沒有啟動,我剛才也檢測過php的進程,的確是沒有php進程,找到:

/usr/local/php/sbin/php-fpm 

開啟后,一切恢復正常!

7、下面介紹一個  nginx 跟 php-fpm 相關的操作

nginx

(1)啟動  

cd usr/local/nginx/sbin
./nginx
cd usr/local/nginx/sbin
./nginx

(2)重啟

  更改配置重啟nginx  

kill -HUP 主進程號或進程號文件路徑
或者使用
cd /usr/local/nginx/sbin
./nginx -s reload

    (3)判斷配置文件是否正確 

nginx -t -c /usr/local/nginx/conf/nginx.conf
或者
cd  /usr/local/nginx/sbin
./nginx -t

(4)關閉

  查詢nginx主進程號

  ps -ef | grep nginx

  從容停止   kill -QUIT 主進程號

  快速停止   kill -TERM 主進程號

  強制停止   kill -9 nginx

  若nginx.conf配置了pid文件路徑,如果沒有,則在logs目錄下

  kill -信號類型 '/usr/local/nginx/logs/nginx.pid'

php-fpm

(1)啟動

/usr/local/php/sbin/php-fpm 

 php-fpm 5.4.7 如何關閉 重啟?

php 5.4.7 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信號控制:

master進程可以理解以下信號

INT, TERM 立刻終止 QUIT 平滑終止 USR1 重新打開日志文件 USR2 平滑重載所有worker進程並重新載入配置和二進制模塊

示例:

php-fpm 關閉:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重啟:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

可能遇到的問題:

登陸服務器之后進到nginx使用./nginx -s reload重新讀取配置文件,發現報nginx: [error] open() "/usr/local/nginx1.9.1/logs/nginx.pid" failed (2: No such file or directory)錯誤,進到logs文件發現的確沒有nginx.pid文件

[root@localhost sbin]# ./nginx -s reload

nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

解決方法:

root@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/nginx1.9.1/sbin#  /usr/local/nginx1.9.1/sbin/nginx -c /usr/local/nginx1.9.1/conf/nginx.conf
root@timeless-HP-Pavilion-g4-Notebook-PC:/usr/local/nginx1.9.1/sbin# ./nginx -s reload

總用量 12

-rw-r--r-- 1 root root 1246 12月 9 18:10 access.log

-rw-r--r-- 1 root root 516 12月 10 15:39 error.log

-rw-r--r-- 1 root root 5 12月 10 15:38 nginx.pid

看nginx.pid文件已經有了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM