在linux服务器下搭建一套web php环境,服务器通常有两种选择,今天给大家介绍下nginx+php的环境搭建。

这里需要声明下,由于每个人的机器多多少少会有些不同,因此在安装过程中也经常会出现A机器安装没问题,而B机器安装过程中却报错现象。以前我在安装一些软件时候,遇到任何错误,就会认为这个安装教程有问题,然后赶紧去找另外的教程重新安装。

其实上面这种做法是错误的,我们应该在安装的时候看懂,为什么要这样安装,每一个参数大概又是有什么用处,带着这样的知识再去安装,就能减少很多困惑了。而且,一般出现错误,往往是系统缺少某个源、引用的某个路径不对、用户或目录权限不对等问题,只要我们耐心些,在google多查阅资料,最终肯定是能安装成功的。

  • 服务器软件依赖安装
  • 下载所需的软件,含Nginx、PHP、MySql软件依赖,以及一些其它常用依赖(如php某些扩展)。

    root@localhost [~]# yum install -y \
    cpp gcc gcc-c++ libgcc libstdc++ libstdc++-devel libgomp \
    libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
    libxml2 libxml2-devel zlib zlib-devel libmcrypt libmcrypt-devel \
    glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
    ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5-devel \
    libidn libidn-devel curl-devel openssl openssl-devel \
    openldap openldap-devel openldap-clients openldap-servers

    安装中,可能会出现libmcrypt找不到错误。解决方法:

    root@localhost [~]# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    root@localhost [~]# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
    root@localhost [~]# rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
    root@localhost [~]# yum install libmcrypt libmcrypt-devel
  • 安装PHP
  • 访问官网下载最新版本php,这里以php5.6为例子。
    注:如果是php7的话,需要将--with-mysql 换成 --with-pdo-mysql。

    root@localhost [~]# wget http://cn2.php.net/distributions/php-5.6.6.tar.gz
    root@localhost [~]# tar -zxvf php-5.6.6.tar.gz
    root@localhost [~]# cd php-5.6.6
    root@localhost [~]# ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php \
    --with-iconv-dir --with-freetype-dir \
    --with-mysql \
    --with-mysqli \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib \
    --with-mhash \
    --enable-sockets \
    --enable-ftp \
    --with-libxml-dir \
    --enable-xml \
    --disable-rpath \
    --enable-bcmath \
    --enable-shmop \
    --enable-sysvsem \
    --enable-inline-optimization \
    --with-curl \
    --enable-mbregex \
    --enable-mbstring \
    --with-mcrypt \
    --with-gd \
    --enable-gd-native-ttf \
    --with-openssl \
    --with-mhash \
    --enable-pcntl \
    --enable-sockets \
    --enable-fpm \
    --with-xmlrpc \
    --enable-zip \
    --enable-soap \
    --without-pear
    root@localhost [~]# make
    root@localhost [~]# make install
    #生成php配置文件
    root@localhost [~]# cp php.ini-production /usr/local/php/php.ini
    #加入到/usr/bin目录下
    root@localhost [~]# ln -s /usr/local/php/bin/php /usr/bin/php
    #php安装完成,执行php -v可以看到运行正常
    root@localhost [~]# php -v

    php安装完成后,我们可以编写一个php文件(如文件包含phpinfo方法),通过php指令来执行文件进行测试,若输出的为php相关配置具体信息,说明php安装成功。

    但是,nginx服务运行的是fpm,因此我们需要启动fpm,具体流程如下:

    root@localhost [~]# useradd nobody
    root@localhost [~]# groupadd nobody
    root@localhost [~]# cd /usr/local/php/
    #生成fpm配置文件
    root@localhost [~]# cp etc/php-fpm.conf.default etc/php-fpm.conf
    root@localhost [~]# vi etc/php-fpm.conf
    #对一些参数配置可以进行修改,也可不修改,未来需要时候再做调整
    pm.max_children = 64
    pm.start_servers = 5
    pm.min_spare_servers = 5
    pm.max_spare_servers = 35
    pm.max_requests = 1024
    #检查配置是否正确
    root@localhost [~]# /usr/local/php/sbin/php-fpm -t
    [17-Jan-2014 11:37:00] NOTICE: configuration file /opt/app/php/etc/php-fpm.conf test is successful
    root@localhost [~]# sbin/php-fpm
    #设置开机启动项
    root@localhost [~]# vi /etc/rc.d/rc.local
    #在行末加入
    /usr/local/php/sbin/php-fpm &

    注:如果是php7的话,php-fpm的具体配置在另外一个文件里,这个文件里面包括了pm等配置:

    root@localhost [~]#
    cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
  • PHP FPM启动操作

  • php5.3.3以前的版本,直接执行./sbin/php-fpm命令,后面加参数(start|stop|reload)来实现重启等功能,新版本已废弃此方法。

    目前新版本php,采用的是信号控制,master进程可以理解以下信号:
    INT, TERM 立刻终止
    QUIT 平滑终止
    USR1 重新打开日志文件
    USR2 平滑重载所有worker进程并重新载入配置和二进制模块

    举例如下:

    #找到master进程,记录其进程号
    root@localhost [~]# ps aux | grep php-fpm
    root     22184  0.0  0.4 204732  4892 ?        Ss   14:49   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
    nobody   22185  0.8  2.9 226200 29764 ?        S    14:49   0:03 php-fpm: pool www
    nobody   22186  0.8  2.6 223680 27084 ?        S    14:49   0:03 php-fpm: pool www
    root     22199  0.0  0.0 103252   836 pts/1    S+   14:55   0:00 grep php-fpm
    #通过信号控制进行相关操作
    #关闭fpm进程
    root@localhost [~]# kill -INT 22184
    #平滑重启fpm进程
    root@localhost [~]# kill -USR2 22184

    上面的操作还是有些复杂的,我们在操作中也应该避免查看进程号并手动输入的习惯,因为万一我们把进程id输错了,很可能对别的程序造成影响。php fpm有一个配置,可以每次启动以后记录当前master id到指定文件,我们可以通过修改该配置,来简化重启等操作,具体如下:

    #修改php-fpm.conf文件,打开pid相关注释
    root@localhost [~]# vi ./etc/php-fpm.conf
    [global]
    ; Pid file
    ; Note: the default prefix is /usr/local/php/var
    ; Default Value: none
    pid = run/php-fpm.pid
    #重启php fpm后,会发现在var目录存在pid的文件了
    root@localhost [~]# ll ./var/run/
    total 4
    -rw-r--r-- 1 root root 5 Mar 24 14:49 php-fpm.pid
    root@localhost [~]# cat ./var/run/php-fpm.pid
    22184
    #后续不再需要查看进程号来操作了,只需要直接读取该pid文件即可
    root@localhost [~]# kill -USR2 `cat ./var/run/php-fpm.pid`
    root@localhost [~]# cat ./var/run/php-fpm.pid
    22218
    #我们发现,pid已经变化,说明平滑重启成功
  • 安装NGINX
  • 依旧是前往官方下载最新版本,这里以nginx1.6为例子,假设安装目录为/opt/app/nginx目录:

    root@localhost [~]# yum -y install pcre-devel
    root@localhost [~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz
    root@localhost [~]# tar -zxvf nginx-1.6.2.tar.gz
    root@localhost [~]# cd nginx-1.6.2
    root@localhost [~]# ./configure --user=nobody --group=nobody \
    --prefix=/opt/app/nginx \
    --sbin-path=/opt/app/nginx/sbin/nginx \
    --conf-path=/opt/app/nginx/conf/nginx.conf \
    --with-http_stub_status_module \
    --with-http_ssl_module \
    --with-http_flv_module \
    --with-http_gzip_static_module \
    --with-pcre \
    --lock-path=/var/run/nginx.lock \
    --pid-path=/var/run/nginx.pid
    root@localhost [~]# make
    root@localhost [~]# make install
    #启动前检查配置是否正确
    root@localhost [~]# /opt/app/nginx/sbin/nginx -t
    nginx: the configuration file /opt/app/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /opt/app/nginx/conf/nginx.conf test is successful
    #启动
    root@localhost [~]# /opt/app/nginx/sbin/nginx
    #重启
    root@localhost [~]# /opt/app/nginx/sbin/nginx -s reload
    #curl测试确认是否正常
    root@localhost [~]# curl localhost
    #设置开机启动
    vi /etc/rc.d/rc.local
    #在行末加入
    /opt/app/nginx/sbin/nginx &
  • Nginx简单的配置
  • nginx.conf文件

    root@localhost [~]# vim /opt/app/nginx/conf/nginx.conf

    user  nobody nobody;
    worker_processes  4;
    worker_rlimit_nofile 51200;

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

    #pid        logs/nginx.pid;


    events {
        use epoll;
        worker_connections  51200;
    }



    http {
        include       mime.types;
        default_type  application/octet-stream;
        include       /opt/app/nginx/conf/fastcgi.conf;
        index         index.html index.htm index.php

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        #access_log  logs/access.log  main;

        sendfile       on;
        tcp_nopush     on;
        tcp_nodelay      on;

        #keepalive_timeout  0;
        keepalive_timeout  65;
       
        gzip  on;
        gzip_min_length  1000;
        gzip_buffers     4 16k;
        gzip_types  text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

        fastcgi_connect_timeout 30;
        fastcgi_send_timeout 900;
        fastcgi_read_timeout 900;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        server {
            listen       80;
            server_name  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html;
                index  index.html index.htm;
            }

            #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   html;
            }

            # 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;
            #}
        }
       
        server {
            listen 80;
            #listen       443 ssl;
            server_name love.ranshy.com;
            root /blog;

            #ssl_certificate       mytest-chain.pem;
            #ssl_certificate_key   mytest-key.pem;

            include server.conf;
            access_log  logs/blog_access.log;
        }

        # 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;
        #    }
        #}

    }

    每一个server配置都引用了server.conf文件,方便统一调整,新建server.conf文件如下:

    root@localhost [~]# vim /opt/app/nginx/conf/server.conf
    #location  ^~ /protected {
    #    rewrite ^(.*)$ /index.php;
    #}

    location ~* .*\.(sqlite|sq3)$ {
        deny all;
    }


    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php;
    }

    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;
    }

    location ~* .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ {
        expires      90d;
        access_log   off;
    }

    location ~* .*\.(js|css)?$ {
        expires      90d;
        access_log   off;
    }

    建立一个php文件,网页访问,最终效果图如下: