CentOS 6.1 環(huán)境中部署nginx、php(包括fastcgi)、虛擬主機配置
更新時間:2012年08月04日 13:52:12 作者:
CentOS 6.1 環(huán)境中部署nginx、php(包括fastcgi)、虛擬主機配置,需要的朋友可以參考下
部署時間:2012-07-24
OS環(huán)境:CentOS 6.1
nginx:nginx-1.2.2
PHP:PHP5.3.14
0、安裝依賴包
1、添加 www 用戶用來執(zhí)行nginx
2、創(chuàng)建臨時目錄
mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx/fcgi/
3、下載nginx最新穩(wěn)定版源代碼
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;
# 進程數(shù)量,nginx作者認為一個就可以,根據(jù)自己的訪問量修改
worker_processes 1;
# 設(shè)置錯誤日志:
#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、建立虛擬機配置文件存放的目錄
這樣配置后,需要新增加虛擬主機的直接在 nginx/conf/sites/目錄下,添加配置文件即可
例如:現(xiàn)在有 www.dhdzp.com 域名
建立:/opt/web/nginx/conf/sites/www.dhdzp.com.conf 文件
內(nèi)容如下:
server {
listen 80;
client_max_body_size 10M;
#多個域名用空格分割,第一個為默認
server_name www.dhdzp.com jb51.net;
charset UTF-8;
index index.html index.htm index.php;
# 定義根目錄
set $root /var/webroot/www.dhdzp.com/;
# 設(shè)置站點路徑
root $root;
# 防止目錄瀏覽
autoindex off;
if ($host != 'www.dhdzp.com') {
rewrite ^/(.*)$ http://www.dhdzp.com/$1 permanent;
}
# 防止.htaccess文件被請求
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www.dhdzp.com/uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# 將其它的請求轉(zhuǎn)交給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類型的請求轉(zhuǎn)交給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.dhdzp.com.log;
# 加載.htaccess重寫文件,注意,這里不支持變量路徑
# 不能寫成 include $root/www.dhdzp.com/.htaccess;
# include /var/webroot/www.dhdzp.com/.htaccess;
# 開啟域名跳轉(zhuǎn),則當訪問出錯后,其他域名會自動跳轉(zhuǎn)到 www.dhdzp.com
# 注意,這里我說的是,僅僅當訪問出錯后,才會跳轉(zhuǎn),所以,這里并不能實現(xiàn)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
執(zhí)行:
如果報錯,可能是你的 autoconf不是 2.13 版本的,PHP5.3.系列的bug,需要安裝 autoconf為2.13的版本:
CentOS : # yum install autoconf213
Debian : # apt-get install autoconf2.13
設(shè)置環(huán)境變量
# CentOS :
export PHP_AUTOCONF="/usr/bin/autoconf-2.13"
# Debian :
export PHP_AUTOCONF="/usr/bin/autoconf2.13"
再次運行:./buildconf --force ,出現(xiàn) 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
啟動nginx
9、測試一下
輸入和保存
<?PHP
phpinfo();
?>
10、在瀏覽器地址欄輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的信息
OS環(huán)境:CentOS 6.1
nginx:nginx-1.2.2
PHP:PHP5.3.14
0、安裝依賴包
復(fù)制代碼 代碼如下:
yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make
1、添加 www 用戶用來執(zhí)行nginx
復(fù)制代碼 代碼如下:
useradd -M -r -s /sbin/nologin -d /opt/web/ www
2、創(chuàng)建臨時目錄
復(fù)制代碼 代碼如下:
mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx/fcgi/
3、下載nginx最新穩(wěn)定版源代碼
復(fù)制代碼 代碼如下:
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.2.2.tar.gz
4、解壓,編譯,安裝
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
vim /opt/web/nginx/conf/nginx.conf
# 指定啟動用戶:
user www www;
# 進程數(shù)量,nginx作者認為一個就可以,根據(jù)自己的訪問量修改
worker_processes 1;
# 設(shè)置錯誤日志:
#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、建立虛擬機配置文件存放的目錄
復(fù)制代碼 代碼如下:
mkdir /opt/web/nginx/conf/sites
這樣配置后,需要新增加虛擬主機的直接在 nginx/conf/sites/目錄下,添加配置文件即可
例如:現(xiàn)在有 www.dhdzp.com 域名
建立:/opt/web/nginx/conf/sites/www.dhdzp.com.conf 文件
內(nèi)容如下:
復(fù)制代碼 代碼如下:
server {
listen 80;
client_max_body_size 10M;
#多個域名用空格分割,第一個為默認
server_name www.dhdzp.com jb51.net;
charset UTF-8;
index index.html index.htm index.php;
# 定義根目錄
set $root /var/webroot/www.dhdzp.com/;
# 設(shè)置站點路徑
root $root;
# 防止目錄瀏覽
autoindex off;
if ($host != 'www.dhdzp.com') {
rewrite ^/(.*)$ http://www.dhdzp.com/$1 permanent;
}
# 防止.htaccess文件被請求
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www.dhdzp.com/uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# 將其它的請求轉(zhuǎn)交給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類型的請求轉(zhuǎn)交給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.dhdzp.com.log;
# 加載.htaccess重寫文件,注意,這里不支持變量路徑
# 不能寫成 include $root/www.dhdzp.com/.htaccess;
# include /var/webroot/www.dhdzp.com/.htaccess;
# 開啟域名跳轉(zhuǎn),則當訪問出錯后,其他域名會自動跳轉(zhuǎn)到 www.dhdzp.com
# 注意,這里我說的是,僅僅當訪問出錯后,才會跳轉(zhuǎn),所以,這里并不能實現(xiàn)301重定向!
server_name_in_redirect on;
}
7、安裝最新版本PHP( PHP5.3.14 )
復(fù)制代碼 代碼如下:
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
執(zhí)行:
復(fù)制代碼 代碼如下:
./buildconf --force
如果報錯,可能是你的 autoconf不是 2.13 版本的,PHP5.3.系列的bug,需要安裝 autoconf為2.13的版本:
復(fù)制代碼 代碼如下:
CentOS : # yum install autoconf213
Debian : # apt-get install autoconf2.13
設(shè)置環(huán)境變量
復(fù)制代碼 代碼如下:
# CentOS :
export PHP_AUTOCONF="/usr/bin/autoconf-2.13"
# Debian :
export PHP_AUTOCONF="/usr/bin/autoconf2.13"
再次運行:./buildconf --force ,出現(xiàn) buildconf: autoconf version 2.13 (ok)
,則表示成功。
編譯安裝 PHP
復(fù)制代碼 代碼如下:
./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 啟用如下幾行,即去掉前面的分號(;)
復(fù)制代碼 代碼如下:
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
復(fù)制代碼 代碼如下:
/opt/web/php/sbin/php-fpm
啟動nginx
復(fù)制代碼 代碼如下:
/opt/web/nginx/sbin/nginx
9、測試一下
復(fù)制代碼 代碼如下:
vim /var/webroot/www.dhdzp.com/tz.php
輸入和保存
復(fù)制代碼 代碼如下:
<?PHP
phpinfo();
?>
10、在瀏覽器地址欄輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的信息
您可能感興趣的文章:
- Nginx中FastCGI如何配置優(yōu)化
- PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法
- 在Mac OS上部署Nginx和FastCGI以及Flask框架的教程
- PHP+FastCGI+Nginx配置PHP運行環(huán)境
- Perl使用nginx FastCGI環(huán)境做WEB開發(fā)實例
- nginx FastCGI錯誤Primary script unknown解決辦法
- Gentoo 下安裝與配置Nginx+ MySQL + PHP (fastcgi) 環(huán)境步驟分享
- Nginx(PHP/fastcgi)的PATH_INFO問題
- Nginx+PHP(FastCGI)搭建高并發(fā)WEB服務(wù)器(自動安裝腳本)第二版
- Nginx0.5.33+PHP5.2.5(FastCGI)搭建勝過Apache10倍的Web服務(wù)器
- Nginx FastCGI緩存的實現(xiàn)示例
相關(guān)文章
Linux內(nèi)核私闖進程地址空間并修改進程內(nèi)存的方法
這篇文章主要介紹了Linux內(nèi)核私闖進程地址空間并修改進程內(nèi)存的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
用shell命令刪除網(wǎng)站最新nb掛馬的方法與代碼
教你刪除網(wǎng)站最新NB掛馬最近發(fā)現(xiàn) 很多網(wǎng)站被掛了木馬,會在頁面中生成如下代碼。2010-02-02
簡單了解linux終端創(chuàng)建文件的2種常用方法
這篇文章主要介紹了簡單了解linux終端創(chuàng)建文件的2種常用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
redhat Server release 5.2 安裝配置簡明教程
系統(tǒng)安裝:系統(tǒng)安裝采用光盤安裝,以前一直從USB移動硬盤安裝,前幾天心血來潮,刻成了DVD,以示嚴肅和一切從頭開始,呵呵。2009-08-08
CentOS 6.5 web服務(wù)器apache的安裝與基本設(shè)置
這篇文章主要介紹了CentOS 6.5 web服務(wù)器apache的安裝與基本設(shè)置,需要的朋友可以參考下2017-09-09

