lnmp環(huán)境中nginx與php是一個重頭戲了,很多朋友在配置這一步時都要折騰很多,在此小編也同樣是折騰了,下面我整理了一篇lnmp環(huán)境中安裝配置Nginx與PHP教程,希望例子可以幫助到大家.
安裝Nginx的方式有很多種,這里我們還是編譯源碼進行安裝,使用下列命令:
- $ wget http://nginx.org/download/nginx-1.6.2.tar.gz
 - $ tar -zxvf nginx-1.6.2.tar.gz
 - $ cd nginx-1.6.2
 - $ ./configure --prefix=/usr/local/nginx
 - $ make
 - $ sudo make install
 
如果安裝過程中出現(xiàn)如下錯誤:
- ./configure: error: the HTTP rewrite module requires the PCRE library.
 - 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.
 
則需要先安裝pcre:
$ sudo yum install pcre-devel
安裝完成之后,我們的Nginx安裝目錄在/usr/local/nginx,接下來修改nginx的配置文件(/usr/local/nginx/conf/nginx.conf),使其能夠處理php腳本.
- worker_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 _;
 - root /vagrant;
 - location / {
 - index index.html index.htm index.php;
 - }
 - location /demo {
 - index index.php;
 - if (!-e $request_filename) {
 - rewrite ^/demo/(.*)$ /demo/index.php?$1 last;
 - break;
 - }
 - }
 - error_page 500 502 503 504 /50x.html;
 - location = /50x.html {
 - root html;
 - }
 - location ~ \.php$ {
 - fastcgi_pass 127.0.0.1:9000;
 - fastcgi_index index.php;
 - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 - include fastcgi_params;
 - }
 - }
 - }
 
最后,啟動Nginx時,需要先啟動PHP-FPM.
- $ sudo /usr/local/php/sbin/php-fpm
 - $ sudo /usr/local/nginx/sbin/nginx
 
對于Nginx的重啟以及關閉操作,可以使用以下命令.
$ sudo /usr/local/nginx/sbin/nginx -s [reload|restart|stop]
而PHP-FPM,則麻煩一點,需要先使用ps -ef|grep php-fpm獲取master process的進程ID,再使用kill -USR2:
- $ ps -ef|grep php-fpm
 - root 6221 1 0 02:17 ? 00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
 - nobody 6222 6221 0 02:17 ? 00:00:00 php-fpm: pool www
 - nobody 6223 6221 0 02:17 ? 00:00:00 php-fpm: pool www
 - vagrant 6233 1623 0 02:18 pts/0 00:00:00 grep php-fpm
 - $ sudo kill -USR2 6221
 
注意:-USR2參數(shù)為重啟,-INT參數(shù)為關閉.
創(chuàng)建虛擬主機:
- [root@os11728 httpd-2.2.22]# vi /usr/local/nginx/conf/vhosts/www_finet230_cn.conf
 
內容如下:
- server {
 - listen 8080;
 - server_name ng.fine230.cn finet85.cn;
 - root /var/www/root/ng_finet230_cn;
 - #激活/關閉自動索引
 - autoindex on;
 - #設定索引時文件大小的單位(B,KB, MB 或 GB)
 - #默認為on,顯示出文件的確切大小,單位是bytes。
 - #改為off后,顯示出文件的大概大小,單位是kB或者MB或者GB
 - autoindex_exact_size off;
 - #開啟以本地時間來顯示文件時間的功能。默認為關(GMT時間)
 - #默認為off,顯示的文件時間為GMT時間。
 - #改為on后,顯示的文件時間為文件的服務器時間
 - autoindex_localtime on;
 - #charset koi8-r;
 - location / {
 - index index.html index.htm index.php;
 - }
 - #error_page 404 /404.html;
 - # redirect server error pages to the static page /50x.html
 - #
 - error_page 500 502 503 504 /50x.html;
 - location = /50x.html {
 - root /var/www/root/ng_finet230_cn;
 - }
 - # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 - #
 - #location ~ \.php$ {
 - # proxy_pass http://127.0.0.1;
 - #}
 - # 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 /scripts$fastcgi_script_name;
 - # include fastcgi_params;
 - #}
 - # deny access to .htaccess files, if Apache’s document root
 - # concurs with nginx’s one
 - #
 - #location ~ /\.ht {
 - # deny all;
 - #}
 - #將客戶端的請求轉交給fastcgi
 - location ~ .*\.(php|php5|shtml)?$ {
 - #root html;
 - fastcgi_pass 127.0.0.1:9000;#這里指定了fastcgi進程偵聽的端口,nginx就是通過這里與php交互的
 - fastcgi_index index.php;
 - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 - include fastcgi_params;
 - }
 - #網(wǎng)站的圖片較多,更改較少,將它們在瀏覽器本地緩存30天
 - location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 - {
 - expires 30d;
 - }
 - #網(wǎng)站會加載很多JS、CSS,將它們在瀏覽器本地緩存1小時
 - location ~ .*\.(js|css)?$
 - {
 - expires 1h;
 - }
 - }
 - # another virtual host using mix of IP-, name-, and port-based configuration
 - #
 - #server {
 - # listen 8000;
 - # listen somename:8080;
 - # server_name somename alias another.alias;
 - # location / {
 - # root html;
 - # index index.html index.htm;
 - # }
 - #}
 - # HTTPS server
 - #
 - #server {
 - # listen 443;
 - # server_name localhost;
 - # ssl on;
 - # ssl_certificate cert.pem;
 - # ssl_certificate_key cert.key;
 - # ssl_session_timeout 5m;
 - # ssl_protocols SSLv2 SSLv3 TLSv1;
 - # ssl_ciphers HIGH:!aNULL:!MD5;
 - # ssl_prefer_server_ciphers on;
 - # location / {
 - # root html;
 - # index index.html index.htm;
 - # } //Vevb.com
 - #}
 - server
 - {
 - listen 8080;
 - server_name status.ng.finet230.cn;
 - location / {
 - stub_status on;
 - access_log off;
 - }
 - }
 
將/var/www/root/ng_finet230_cn目錄下的所有檔案與子目錄的擁有者皆設為www群體的使用者www:
[root@os11728 ~]# chown -R www:www /var/www/root/ng_finet230_cn
1.5.Nginx的啟動與關閉
啟動Nginx:
- [root@os11728 ~]# ulimit -SHn 65535
 - root@os11728 ~]# /usr/local/nginx/sbin/nginx
 
停止Nginx:
- [root@os11728 ~]# /usr/local/nginx/sbin/nginx -s stop
 - //或
 - [root@os11728 ~]# /usr/local/nginx/sbin/nginx -s quit
 
重啟Nginx:
- [root@os11728 ~]# /usr/local/nginx/sbin/nginx -s reload
 - //或
 - [root@os11728 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
 
配置開機自動啟動Nginx + PHP,代碼如下:
[root@os11728 ~]# vi /etc/rc.local
在末尾增加以下內容:
- ulimit -SHn 65535
 - /usr/local/php/sbin/php-fpm
 - /usr/local/nginx/sbin/nginx
 
新聞熱點
疑難解答
圖片精選