诚信为人之本 發表於 2023-11-16 00:00:00

CentOS 6.1 环境中部署nginx、php(包括fastcgi)、虚拟主机配置

<p>部署环境:CentOS 6.1<br/>
        nginx:nginx-1.2.2<br/>
        PHP:PHP5.3.14<br/>
        0、安装依赖包</p><p class="codebody">&nbsp; &nbsp;
        yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make</p><p><br/>
        1、添加 www 用户用来执行nginx</p><p class="codebody">&nbsp; &nbsp;
        useradd -M -r -s /sbin/nologin -d /opt/web/ www</p><p><br/>
        2、创建临时目录</p><p class="codebody"><br/>
        mkdir -p /var/tmp/nginx/client/<br/>
        mkdir -p /var/tmp/nginx/proxy/<br/>
        mkdir -p /var/tmp/nginx/fcgi/</p><p><br/>
        3、下载nginx最新稳定版源代码</p><p class="codebody"><br/>
        cd /usr/local/src/<br/>
        wget http://nginx.org/download/nginx-1.2.2.tar.gz</p><p><br/>
        4、解压,编译,安装</p><p class="codebody"><br/>
        tar vxzf nginx-1.2.2.tar.gz<br/>
        cd nginx-1.2.2/<br/>
        ./configure \<br/>
        --prefix=/opt/web/nginx \<br/>
        --error-log-path=/var/log/nginx/error.log \<br/>
        --pid-path=/var/run/nginx/nginx.pid \<br/>
        --lock-path=/var/lock/nginx.lock \<br/>
        --user=www \<br/>
        --group=www \<br/>
        --with-http_ssl_module \<br/>
        --with-http_stub_status_module \<br/>
        --with-http_gzip_static_module \<br/>
        --http-log-path=/var/log/nginx/access.log \<br/>
        --http-client-body-temp-path=/var/tmp/nginx/client/ \<br/>
        --http-proxy-temp-path=/var/tmp/nginx/proxy/ \<br/>
        --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \<br/>
        --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/<br/>
        make<br/>
        make install</p><p><br/>
        5、配置nginx</p><p class="codebody"><br/>
        vim /opt/web/nginx/conf/nginx.conf<br/>
        # 指定启动用户:<br/>
        user www www;<br/>
        # 进程数量,nginx作者认为一个就可以,根据自己的访问量修改<br/>
        worker_processes 1;<br/>
        # 设置错误日志:<br/>
        #error_log logs/error.log notice;<br/>
        #error_log logs/error.log info;<br/>
        error_log /var/log/nginx/error.default.log;<br/>
        pid /opt/web/nginx/nginx.pid;<br/>
        events {<br/>
        use epoll;<br/>
        worker_connections 1024;<br/>
        }<br/>
        http {<br/>
        charset utf-8;<br/>
        include mime.types;<br/>
        default_type application/octet-stream;<br/>
        #log_format main &#39;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#39;<br/>
        # &#39;$status $body_bytes_sent &quot;$http_referer&quot; &#39;<br/>
        # &#39;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#39;;<br/>
        #access_log logs/access.log main;<br/>
        sendfile on;<br/>
        tcp_nopush on;<br/>
        tcp_nodelay on;<br/>
        #keepalive_timeout 0;<br/>
        keepalive_timeout 65;<br/>
        gzip on;<br/>
        gzip_min_length 1000;<br/>
        gzip_proxied any;<br/>
        gzip_types text/plain text/css text/xml<br/>
        application/x-javascript application/xml<br/>
        application/atom+xml text/javascript;<br/>
        server {<br/>
        listen 80;<br/>
        server_name localhost;<br/>
        charset utf-8;<br/>
        #access_log logs/host.access.log main;<br/>
        location / {<br/>
        root html;<br/>
        index index.html index.htm;<br/>
        }<br/>
        #error_page 404 /404.html;<br/>
        # redirect server error pages to the static page /50x.html<br/>
        #<br/>
        error_page 500 502 503 504 /50x.html;<br/>
        location = /50x.html {<br/>
        root html;<br/>
        }<br/>
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80<br/>
        #<br/>
        #location ~ \.php$ {<br/>
        # proxy_pass http://127.0.0.1;<br/>
        #}<br/>
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000<br/>
        #<br/>
        location ~ \.php$ {<br/>
        root html;<br/>
        fastcgi_pass 127.0.0.1:9000;<br/>
        fastcgi_index index.php;<br/>
        #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;<br/>
        #include fastcgi_params;<br/>
        include fastcgi.conf;<br/>
        }<br/>
        # deny access to .htaccess files, if Apache&#39;s document root<br/>
        # concurs with nginx&#39;s one<br/>
        #<br/>
        location ~ /\.ht {<br/>
        deny all;<br/>
        }<br/>
        }<br/>
        # another virtual host using mix of IP-, name-, and port-based configuration<br/>
        #<br/>
        #server {<br/>
        # listen 8000;<br/>
        # listen somename:8080;<br/>
        # server_name somename alias another.alias;<br/>
        # location / {<br/>
        # root html;<br/>
        # index index.html index.htm;<br/>
        # }<br/>
        #}<br/>
        # HTTPS server<br/>
        #<br/>
        #server {<br/>
        # listen 443;<br/>
        # server_name localhost;<br/>
        # ssl on;<br/>
        # ssl_certificate cert.pem;<br/>
        # ssl_certificate_key cert.key;<br/>
        # ssl_session_timeout 5m;<br/>
        # ssl_protocols SSLv2 SSLv3 TLSv1;<br/>
        # ssl_ciphers HIGH:!aNULL:!MD5;<br/>
        # ssl_prefer_server_ciphers on;<br/>
        # location / {<br/>
        # root html;<br/>
        # index index.html index.htm;<br/>
        # }<br/>
        #}<br/>
        proxy_read_timeout 200;<br/>
        # Only retry if there was a communication error, not a timeout<br/>
        # on the Tornado server (to avoid propagating &quot;queries of death&quot;<br/>
        # to all frontends)<br/>
        proxy_next_upstream error;<br/>
        proxy_set_header X-Scheme $scheme;<br/>
        proxy_set_header X-Real-IP $remote_addr;<br/>
        proxy_set_header Host $host;<br/>
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br/>
        # 引入虚拟主机文件<br/>
        include /opt/web/nginx/conf/sites/*.conf;<br/>
        }</p><p><br/>
        6、建立虚拟机配置文件存放的目录</p><p class="codebody">&nbsp; &nbsp;
        mkdir /opt/web/nginx/conf/sites</p><p><br/>
        这样配置后,需要新增加虚拟主机的直接在 nginx/conf/sites/目录下,添加配置文件即可<br/>
        例如:现在有 www.jb51.net 域名<br/>
        建立:/opt/web/nginx/conf/sites/www.jb51.net.conf 文件<br/>
        内容如下:</p><p class="codebody"><br/>
        server {<br/>
        listen 80;<br/>
        client_max_body_size 10M;<br/>
        #多个域名用空格分割,第一个为默认<br/>
        server_name www.jb51.net jb51.net;<br/>
        charset UTF-8;<br/>
        index index.html index.htm index.php;<br/>
        # 定义根目录<br/>
        set $root /var/webroot/www.jb51.net/;<br/>
        # 设置站点路径<br/>
        root $root;<br/>
        # 防止目录浏览<br/>
        autoindex off;<br/>
        if ($host != &#39;www.jb51.net&#39;) {<br/>
        rewrite ^/(.*)$ https://www.jb51.net/$1 permanent;<br/>
        }<br/>
        # 防止.htaccess文件被请求<br/>
        location ~ /\.ht {<br/>
        deny all;<br/>
        }<br/>
        error_page 404 /404.html;<br/>
        index index.html index.htm;<br/>
        location /uploads/ {<br/>
        alias /data/webroot/www.jb51.net/uploads/;<br/>
        }<br/>
        try_files $uri @uwsgi;<br/>
        location @uwsgi{<br/>
        # 将其它的请求转交给uwsgi<br/>
        include uwsgi_params;<br/>
        uwsgi_pass unix:/tmp/360ito_uwsgi.sock;<br/>
        proxy_set_header X-Real-IP $remote_addr;<br/>
        proxy_set_header Host $host;<br/>
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br/>
        #proxy_pass http://localhost:5000;<br/>
        }<br/>
        # 将php类型的请求转交给fastcgi<br/>
        location ~ \.php$ {<br/>
        root html;<br/>
        fastcgi_pass 127.0.0.1:9000;<br/>
        fastcgi_index index.php;<br/>
        include fastcgi.conf;<br/>
        }<br/>
        # 访问日志:<br/>
        access_log /var/log/nginx/access.www.jb51.net.log;<br/>
        # 加载.htaccess重写文件,注意,这里不支持变量路径<br/>
        # 不能写成 include $root/www.jb51.net/.htaccess;<br/>
        # include /var/webroot/www.jb51.net/.htaccess;<br/>
        # 开启域名跳转,则当访问出错后,其他域名会自动跳转到 www.jb51.net<br/>
        # 注意,这里我说的是,仅仅当访问出错后,才会跳转,所以,这里并不能实现301重定向!<br/>
        server_name_in_redirect on;<br/>
        }</p><p><br/>
        7、安装最新版本PHP( PHP5.3.14 )</p><p class="codebody"><br/>
        cd /usr/local/src/<br/>
        wget http://cn.php.net/get/php-5.3.14.tar.bz2/from/this/mirror<br/>
        tar xjvf php-5.3.14.tar.bz2<br/>
        cd php-5.3.14</p><p><br/>
        执行:</p><p class="codebody">&nbsp; &nbsp;
        ./buildconf --force</p><p><br/>
        如果报错,可能是你的 autoconf不是 2.13 版本的,PHP5.3.系列的bug,需要安装 autoconf为2.13的版本:</p><p class="codebody"><br/>
        CentOS : # yum install autoconf213<br/>
        Debian : # apt-get install autoconf2.13</p><p><br/>
        设置环境变量</p><p class="codebody"><br/>
        # CentOS :<br/>
        export PHP_AUTOCONF=&quot;/usr/bin/autoconf-2.13&quot;<br/>
        # Debian :<br/>
        export PHP_AUTOCONF=&quot;/usr/bin/autoconf2.13&quot;</p><p><br/>
        再次运行:./buildconf --force ,出现 buildconf: autoconf version 2.13 (ok)<br/>
        ,则表示成功。<br/>
        编译安装 PHP</p><p class="codebody"><br/>
        ./configure \<br/>
        --prefix=/opt/web/php \<br/>
        --with-config-file-path=/opt/web/php/etc \<br/>
        --with-config-file-scan-dir=/opt/web/php/etc/conf.d \<br/>
        --enable-fpm \<br/>
        --with-fpm-user=www \<br/>
        --with-fpm-group=www \<br/>
        --with-mysql=/opt/db/Percona-Server-5.5.14-rel20.5 \<br/>
        --with-mysqli=/opt/db/Percona-Server-5.5.14-rel20.5/bin/mysql_config \<br/>
        --with-iconv-dir \<br/>
        --with-freetype-dir \<br/>
        --with-jpeg-dir \<br/>
        --with-png-dir \<br/>
        --with-zlib \<br/>
        --with-libxml-dir \<br/>
        --enable-xml \<br/>
        --enable-mbstring \<br/>
        --with-gd \<br/>
        --enable-gd-native-ttf \<br/>
        --with-openssl \<br/>
        --enable-inline-optimization<br/>
        make &amp;&amp; make install<br/>
        cp php.ini-production /opt/web/php/etc/php.ini<br/>
        cd /opt/web/php/etc<br/>
        cp php-fpm.conf.default php-fpm.conf</p><p><br/>
        修改php-fpm.conf 启用如下几行,即去掉前面的分号(;)</p><p class="codebody"><br/>
        pid = run/php-fpm.pid<br/>
        error_log = log/php-fpm.log<br/>
        log_level = notice<br/>
        listen = 127.0.0.1:9000<br/>
        listen.allowed_clients = 127.0.0.1<br/>
        listen.owner = www<br/>
        listen.group = www<br/>
        listen.mode = 0666<br/>
        user = www<br/>
        group = www<br/>
        pm = dynamic<br/>
        pm.max_children = 50<br/>
        pm.start_servers = 5<br/>
        pm.min_spare_servers = 5<br/>
        pm.max_spare_servers = 35<br/>
        pm.max_requests = 500<br/>
        env = $HOSTNAME<br/>
        env = /usr/local/bin:/usr/bin:/bin<br/>
        env = /tmp<br/>
        env = /tmp<br/>
        env = /tmp</p><p><br/>
        8、启动php-fpm</p><p class="codebody">&nbsp; &nbsp;
        /opt/web/php/sbin/php-fpm</p><p><br/>
        启动nginx</p><p class="codebody">&nbsp; &nbsp;
        /opt/web/nginx/sbin/nginx</p><p><br/>
        9、测试一下</p><p class="codebody">&nbsp; &nbsp;
        vim /var/webroot/www.jb51.net/tz.php</p><p><br/>
        输入和保存</p><p class="codebody"><br/>&lt;?PHP<br/>
        phpinfo();<br/>
        ?&gt;</p><p><br/>
        10、在浏览器地址栏输入:http://php.jb51.net/tz.php<br/>
        成功的话,可以看到phpinfo()输出的信息</p>
頁: [1]
查看完整版本: CentOS 6.1 环境中部署nginx、php(包括fastcgi)、虚拟主机配置