ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

ubuntu下nginx+php5的部署

2021-04-09 22:52:54  阅读:220  来源: 互联网

标签:ubuntutest01 KVM nginx ubuntu php5 php fastcgi


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

最后,验证访问是否正常:

 

 

 

标签:ubuntutest01,KVM,nginx,ubuntu,php5,php,fastcgi
来源: https://blog.51cto.com/u_10272167/2695551

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有