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

技术教程 2026-01-08 15:01:54 浏览

部署环境:CentOS 6.1nginx:nginx-1.2.2PHP:PHP5.3.140、安装依赖包

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

4、解压,编译,安装

tar vxzf nginx-1.2.2.tar.gzcd 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/makemake 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/xmlapplication/x-javascript application/xmlapplication/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}# 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.jb51.net 域名建立:/opt/web/nginx/conf/sites/www.jb51.net.conf 文件内容如下:

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

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

cd /usr/local/src/wgetxjvf php-5.3.14.tar.bz2cd php-5.3.14

执行:

./buildconf --force

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

CentOS : # yum install autoconf213Debian : # 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-optimizationmake && make installcp php.ini-production /opt/web/php/etc/php.inicd /opt/web/php/etccp php-fpm.conf.default php-fpm.conf

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

pid = run/php-fpm.piderror_log = log/php-fpm.loglog_level = noticelisten = 127.0.0.1:9000listen.allowed_clients = 127.0.0.1listen.owner = wwwlisten.group = wwwlisten.mode = 0666user = wwwgroup = wwwpm = dynamicpm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500env[HOSTNAME] = $HOSTNAMEenv[PATH] = /usr/local/bin:/usr/bin:/binenv[TMP] = /tmpenv[TMPDIR] = /tmpenv[TEMP] = /tmp

8、启动php-fpm

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

启动nginx

/opt/web/nginx/sbin/nginx

9、测试一下

虚拟主机

vim /var/webroot/www.jb51.net/tz.php

输入和保存

10、在浏览器地址栏输入:成功的话,可以看到phpinfo()输出的信息

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐