ubuntu下nginx+php5的部署

散盡浮華發表於2016-12-22

 

ubuntu下nginx+php5環境的部署和centos系統下的部署稍有不同,廢話不多說,以下為操作記錄:
1)nginx安裝
root@ubuntutest01-KVM:~# sudo apt-get update && sudo apt-get upgrade
root@ubuntutest01-KVM:~# sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential openssl libssl0.9.8 libssl-dev
root@ubuntutest01-KVM:~# wget http://nginx.org/download/nginx-1.8.0.tar.gz
root@ubuntutest01-KVM:~# tar -zxvf nginx-1.8.0.tar.gz
root@ubuntutest01-KVM:~# cd nginx-1.8.0
root@ubuntutest01-KVM:~# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
root@ubuntutest01-KVM:~# make && make install

2)php5的安裝
add-apt-repository 命令是 apt 源的新增,ppa 就是軟體對應的源,在官網上可以找到
root@ubuntutest01-KVM:~# sudo add-apt-repository ppa:ondrej/php5-5.6

如果上面命令執行後報錯和沒有發現命令則執行
root@ubuntutest01-KVM:~# sudo apt-get install Python-software-properties
root@ubuntutest01-KVM:~# sudo apt-get update
root@ubuntutest01-KVM:~# sudo apt-get install php
root@ubuntutest01-KVM:~# php5 -v

安裝好php後,在nginx裡新增對接php的配置後,訪問.php檔案會報錯502!
這是因為nginx中訪問.php檔案的請求都交給php-fpm程式處理的,php-fpm監聽9000埠
所以還有啟動php-fpm程式。

安裝php-fpm
root@ubuntutest01-KVM:~# apt-get install php5-fpm php5-gd php5-cli php5-curl php5-mcrypt php5-mysql php5-readline

啟動php-fpm
root@ubuntutest01-KVM:~# service php5-fpm start
root@ubuntutest01-KVM:~# ps -ef|grep php5-fpm
root@ubuntutest01-KVM:~# lsof -i:9000

但是發現php5-fpm啟動後,9000埠卻沒有起來!這是為什麼?
這是因為php-fpm有兩種監聽方式:一種是.sock檔案方式,另一種是9000埠方式
修改辦法:
root@ubuntutest01-KVM:~# vim /etc/php5/fpm/pool.d/www.conf
.....
;listen = /var/run/php5-fpm.sock               //註釋這行,這是預設的監聽方式
listen = 9000                                            //改為監聽9000埠方式

重啟php-fpm
root@ubuntutest01-KVM:~# service php5-fpm restart
root@ubuntutest01-KVM:~# lsof -i:9000        //發現9000埠已經起來了

3)nginx+php配置
root@ubuntutest01-KVM:~# vim /usr/local/nginx/conf/nginx.conf       //將nginx啟動使用者改成www-data,確保這個使用者存在,不存在就手動建立,保證nginx和php啟動使用者一致

user  www-data;
worker_processes  8;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {
    worker_connections  65535;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset utf-8;
  
    log_format  main  '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_cookie" $host $request_time';
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
  
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
   
    client_header_timeout 600s;
    client_body_timeout 600s;
   
    client_max_body_size 100m;            
    client_body_buffer_size 256k;          
   
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
    gzip_vary on;
 
    include vhosts/*.conf;
}

root@ubuntutest01-KVM:~# vim /usr/local/nginx/conf/vhosts/vote.com.conf

server {
    listen 8080;
    server_name www.wangshibo.com;

    access_log  /usr/local/nginx/logs/access.log main;
    error_log  /usr/local/nginx/logs/error.log;

    location / {
        root /home/www/vote;
        try_files $uri $uri/ @router;
        index index.html index.php index.htm;
        }

    location /nginx_status {
        stub_status on;
        access_log off;
        }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
        }

    location @router {
       rewrite ^.*$ /index.php last;
    }

    location ~ \.php$ {
        root          /home/www/vote;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;         //這一行和下面一行要加上,不然訪問php檔案可能出現空白!
        fastcgi_param  PATH_INFO  $fastcgi_script_name;
        include        fastcgi_params;
    }
    
    location ~ ^/(status|ping)$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
        }
}

測試檔案(html檔案和php檔案)
root@ubuntutest01-KVM:~# vim /home/www/vote/test.html
sdfsadf
12313123
root@ubuntutest01-KVM:~# vim /home/www/vote/test.php

<?php              //這一行不能空格,否則訪問會有問題
phpinfo();
?>

修改php-fpm檔案(確保/etc/php5/fpm/php-fpm.conf檔案中開啟了include=/etc/php5/fpm/pool.d/*.conf)
root@ubuntutest01-KVM:~# vim /etc/php5/fpm/pool.d/www.conf
....
user = www-data
group = www-data
....
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

重啟nginx和php-rpm
root@ubuntutest01-KVM:~# /usr/local/nginx/sbin/nginx -s reload
root@ubuntutest01-KVM:~# /etc/init.d/php5-fpm restart

最後,驗證訪問是否正常:

 

 

 

相關文章