360ITO技术社区
  • 首页
  • 文章
  • 快讯
  • 讨论
  • 问答
  • 小贴士
  • 代码块
  • 开源
  • 老论坛
登录 | 注册

360ITO技术社区  > 文章

订阅文章

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

无相 发布于 8年前 ( comment 2条评论  查看:15418  收藏:0 )

部署时间:2012-07-24
OS环境:CentOS 6.1
nginx:nginx-1.2.2
PHP:PHP5.3.14
       

0、安装依赖包

yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make

1、添加 www 用户用来执行nginx

useradd -M -r -s /sbin/nologin -d /opt/web/ www

2、创建临时目录

mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx/fcgi/

3、下载nginx最新稳定版源代码

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.2.2.tar.gz

4、解压,编译,安装

tar vxzf nginx-1.2.2.tar.gz
cd nginx-1.2.2/
          
./configure \
  --prefix=/opt/web/nginx \
  --error-log-path=/var/log/nginx/error.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=www \
  --group=www \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-log-path=/var/log/nginx/access.log \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/
          
make
make install

5、配置nginx

vim /opt/web/nginx/conf/nginx.conf
# 指定启动用户:
user www www;
          
# 进程数量,nginx作者认为一个就可以,根据自己的访问量修改
worker_processes  1;
          
# 设置错误日志:
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log /var/log/nginx/error.default.log;
          
pid /opt/web/nginx/nginx.pid;
          
          
events {
    use epoll;
    worker_connections  1024;
}
          
          
http {
    charset       utf-8;
          
    include       mime.types;
    default_type  application/octet-stream;
          
    #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_proxied any;
    gzip_types text/plain text/css text/xml
               application/x-javascript application/xml
               application/atom+xml text/javascript;
          
    server {
        listen       80;
        server_name  localhost;
          
        charset utf-8;
          
        #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;
            include fastcgi.conf;
        }
          
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }
          
          
    # 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;
    #    }
    #}
          
    proxy_read_timeout 200;
          
    # Only retry if there was a communication error, not a timeout
    # on the Tornado server (to avoid propagating "queries of death"
    # to all frontends)
    proxy_next_upstream error;
          
    proxy_set_header X-Scheme $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          
          
    # 引入虚拟主机文件
    include /opt/web/nginx/conf/sites/*.conf;
}

6、建立虚拟机配置文件存放的目录

mkdir /opt/web/nginx/conf/sites

这样配置后,需要新增加虚拟主机的直接在 nginx/conf/sites/目录下,添加配置文件即可
例如:现在有 www.360ito.com 域名
建立:/opt/web/nginx/conf/sites/www.360ito.com.conf 文件
内容如下:

server {
    listen 80;
    client_max_body_size 10M;
         
    #多个域名用空格分割,第一个为默认
    server_name www.360ito.com 360ito.com;  
         
    charset UTF-8;
    index index.html index.htm index.php;
         
    # 定义根目录
    set $root /var/webroot/www.360ito.com/;
         
    # 设置站点路径
    root  $root;   
         
    # 防止目录浏览
    autoindex off;
         
    if ($host != 'www.360ito.com') {
        rewrite ^/(.*)$ http://www.360ito.com/$1 permanent;
    }
         
    # 防止.htaccess文件被请求
    location ~ /\.ht {
    deny all;
    }
         
    error_page  404              /404.html;
         
    index  index.html index.htm;   
         
    location /uploads/ {
        alias /data/webroot/www.360ito.com/uploads/;
    }
         
    try_files $uri @uwsgi; 
    location @uwsgi{
        # 将其它的请求转交给uwsgi
        include uwsgi_params;
        uwsgi_pass   unix:/tmp/360ito_uwsgi.sock;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_pass http://localhost:5000;
    }
         
    # 将php类型的请求转交给fastcgi
    location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    include fastcgi.conf;
    }
         
    # 访问日志:
    access_log  /var/log/nginx/access.www.360ito.com.log;
          
    # 加载.htaccess重写文件,注意,这里不支持变量路径
    # 不能写成 include $root/www.360ito.com/.htaccess;
    # include /var/webroot/www.360ito.com/.htaccess;
         
    # 开启域名跳转,则当访问出错后,其他域名会自动跳转到 www.360ito.com
    # 注意,这里我说的是,仅仅当访问出错后,才会跳转,所以,这里并不能实现301重定向!
    server_name_in_redirect  on;
}

7、安装最新版本PHP( PHP5.3.14 )

cd /usr/local/src/
wget http://cn.php.net/get/php-5.3.14.tar.bz2/from/this/mirror
tar xjvf php-5.3.14.tar.bz2 
cd php-5.3.14

执行:

./buildconf --force

如果报错,可能是你的 autoconf不是 2.13 版本的,PHP5.3.系列的bug,需要安装 autoconf为2.13的版本:

CentOS : # yum install autoconf213 
Debian : # apt-get install autoconf2.13

设置环境变量

# CentOS : 
export PHP_AUTOCONF="/usr/bin/autoconf-2.13"
# Debian : 
export PHP_AUTOCONF="/usr/bin/autoconf2.13"

再次运行:./buildconf --force ,出现 buildconf: autoconf version 2.13 (ok)
,则表示成功。
编译安装 PHP

./configure \
  --prefix=/opt/web/php \
  --with-config-file-path=/opt/web/php/etc \
  --with-config-file-scan-dir=/opt/web/php/etc/conf.d \
  --enable-fpm \
  --with-fpm-user=www \
  --with-fpm-group=www \
  --with-mysql=/opt/db/Percona-Server-5.5.14-rel20.5 \
  --with-mysqli=/opt/db/Percona-Server-5.5.14-rel20.5/bin/mysql_config \
  --with-iconv-dir \
  --with-freetype-dir \
  --with-jpeg-dir \
  --with-png-dir \
  --with-zlib \
  --with-libxml-dir \
  --enable-xml \
  --enable-mbstring \
  --with-gd \
  --enable-gd-native-ttf \
  --with-openssl \
  --enable-inline-optimization 
         
make && make install
cp php.ini-production /opt/web/php/etc/php.ini
cd /opt/web/php/etc
cp php-fpm.conf.default php-fpm.conf

修改php-fpm.conf 启用如下几行,即去掉前面的分号(;)

pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5 
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

       
8、启动php-fpm

/opt/web/php/sbin/php-fpm

启动nginx

/opt/web/nginx/sbin/nginx

       

9、测试一下

vim /var/webroot/www.360ito.com/tz.php

输入和保存

<?PHP
    phpinfo();
?>
10、在浏览器地址栏输入:http://www.360ito.com/tz.php

成功的话,可以看到phpinfo()输出的信息

0 有用 0 无用
本站原创,欢迎转载;尊重他人劳动,转载时保留以下信息:
本文转自:360ITO技术社区
原文标题:CentOS 6.1 环境中部署nginx、php(包括fastcgi)、虚拟主机配置
原文地址:http://www.360ito.com/article/7.html
360ito.com
工作笔记 php fastcgi nginx Linux Centos

共有2个评论 我要评论»

按时间排 按有用数排 按支持数排
无相
无相 8年前 | [引述]  [点评] 
0

老码农

一个vps系统,是CentOS 6.2,出现:

configure: error: xml2-config not found.

安装:libxml2-devel yum install libxml2-devel
老码农
老码农 8年前 | [引述]  [点评] 
0

一个vps系统,是CentOS 6.2,出现:

configure: error: xml2-config not found.

网友回复/评论仅代表其个人看法,并不表明本社区同意其观点或证实其描述。

请尽量让自己的回复能够对别人有帮助

1.不欢迎无意义的回复/评论和类似“顶”、“沙发”之类没有营养的文字
如果只是想简单的表个态,请点 有用无用支持反对 等按钮
2.发言之前请再仔细看一遍文章,或许是您遗漏、误解了,理性讨论、切莫乱喷
3.严禁发布违法、违规的信息,请勿到处招贴广告、发布软文;
4.如果您发现自己的回复/评论不见了,请参考以上3条
5.不停制造违规、垃圾信息的,账户将被禁止

热门标签

  • android 20
  • Flash 15
  • 游戏 12
  • Linux 12
  • Python 11
  • 工作笔记 11
  • 社交游戏 7
  • delphi 5
  • jquery 5
  • 编程 4
  • 谷歌 4
  • git 4
  • Centos 4
  • JavaScript 3
  • 开发者 3
  • C/C++ 3
  • 安全 2
  • 代码 2
  • 浏览器 2
  • 移动应用 2

相关文章

周热点

  • 5天前qifei233333333333 (65)

月热点

  • 4周前qifei233333333333 (229)
  • 3周前qifei233333333333 (153)
  • 2周前qifei233333333333 (143)
  • 1周前qifei233333333333 (113)
  • 1周前qifei233333333333 (92)
  • 5天前qifei233333333333 (65)
Copyright ©2011-2012 360ITO技术社区 All Rights Reserved. | 关于 | 联系我们 | 杭州精创信息技术有限公司 浙ICP备09019653号-26|
▲