CentOS 6.1 環(huán)境中部署nginx、php(包括fastcgi)、虛擬主機(jī)配置
OS環(huán)境:CentOS 6.1
nginx:nginx-1.2.2
PHP:PHP5.3.14
0、安裝依賴包
1、添加 www 用戶用來(lái)執(zhí)行nginx
2、創(chuàng)建臨時(shí)目錄
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
# 指定啟動(dòng)用戶:
user www www;
# 進(jìn)程數(shù)量,nginx作者認(rèn)為一個(gè)就可以,根據(jù)自己的訪問(wèn)量修改
worker_processes 1;
# 設(shè)置錯(cuò)誤日志:
#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;
# 引入虛擬主機(jī)文件
include /opt/web/nginx/conf/sites/*.conf;
}
6、建立虛擬機(jī)配置文件存放的目錄
這樣配置后,需要新增加虛擬主機(jī)的直接在 nginx/conf/sites/目錄下,添加配置文件即可
例如:現(xiàn)在有 www.dbjr.com.cn 域名
建立:/opt/web/nginx/conf/sites/www.dbjr.com.cn.conf 文件
內(nèi)容如下:
server {
listen 80;
client_max_body_size 10M;
#多個(gè)域名用空格分割,第一個(gè)為默認(rèn)
server_name www.dbjr.com.cn jb51.net;
charset UTF-8;
index index.html index.htm index.php;
# 定義根目錄
set $root /var/webroot/www.dbjr.com.cn/;
# 設(shè)置站點(diǎn)路徑
root $root;
# 防止目錄瀏覽
autoindex off;
if ($host != 'www.dbjr.com.cn') {
rewrite ^/(.*)$ http://www.dbjr.com.cn/$1 permanent;
}
# 防止.htaccess文件被請(qǐng)求
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www.dbjr.com.cn/uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# 將其它的請(qǐng)求轉(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類型的請(qǐng)求轉(zhuǎn)交給fastcgi
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 訪問(wèn)日志:
access_log /var/log/nginx/access.www.dbjr.com.cn.log;
# 加載.htaccess重寫文件,注意,這里不支持變量路徑
# 不能寫成 include $root/www.dbjr.com.cn/.htaccess;
# include /var/webroot/www.dbjr.com.cn/.htaccess;
# 開啟域名跳轉(zhuǎn),則當(dāng)訪問(wèn)出錯(cuò)后,其他域名會(huì)自動(dòng)跳轉(zhuǎn)到 www.dbjr.com.cn
# 注意,這里我說(shuō)的是,僅僅當(dāng)訪問(wèn)出錯(cuò)后,才會(huì)跳轉(zhuǎn),所以,這里并不能實(shí)現(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í)行:
如果報(bào)錯(cuò),可能是你的 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"
再次運(yùn)行:./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 啟用如下幾行,即去掉前面的分號(hào)(;)
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、啟動(dòng)php-fpm
啟動(dòng)nginx
9、測(cè)試一下
輸入和保存
<?PHP
phpinfo();
?>
10、在瀏覽器地址欄輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的信息
- Nginx中FastCGI如何配置優(yōu)化
- PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯(cuò)誤的解決方法
- 在Mac OS上部署Nginx和FastCGI以及Flask框架的教程
- PHP+FastCGI+Nginx配置PHP運(yùn)行環(huán)境
- Perl使用nginx FastCGI環(huán)境做WEB開發(fā)實(shí)例
- nginx FastCGI錯(cuò)誤Primary script unknown解決辦法
- Gentoo 下安裝與配置Nginx+ MySQL + PHP (fastcgi) 環(huán)境步驟分享
- Nginx(PHP/fastcgi)的PATH_INFO問(wèn)題
- Nginx+PHP(FastCGI)搭建高并發(fā)WEB服務(wù)器(自動(dòng)安裝腳本)第二版
- Nginx0.5.33+PHP5.2.5(FastCGI)搭建勝過(guò)Apache10倍的Web服務(wù)器
- Nginx FastCGI緩存的實(shí)現(xiàn)示例
相關(guān)文章
Linux中SELinux三種模式的啟動(dòng)、關(guān)閉與查看方式
這篇文章主要介紹了Linux中SELinux三種模式的啟動(dòng)、關(guān)閉與查看方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Linux內(nèi)核設(shè)備驅(qū)動(dòng)之內(nèi)核中鏈表的使用筆記整理
今天小編就為大家分享一篇關(guān)于Linux內(nèi)核設(shè)備驅(qū)動(dòng)之內(nèi)核中鏈表的使用筆記整理,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12CentOS 6.5上編譯安裝Apache服務(wù)器的方法(最小化安裝)
這篇文章主要介紹了CentOS 6.5上編譯安裝Apache服務(wù)器的方法(最小化安裝),需要的朋友可以參考下2017-09-09CentOS服務(wù)器+監(jiān)控寶SNMP監(jiān)控全攻略分享
很多人和Sudu一樣都想使用監(jiān)控寶去監(jiān)控自己的linux服務(wù)器,但是因?yàn)榘惭bsnmp存在一些問(wèn)題導(dǎo)致無(wú)法成功設(shè)置snmp的設(shè)置。2010-12-12Linux 出現(xiàn)telnet: 127.0.0.1: Connection refused錯(cuò)誤解決辦法
這篇文章主要介紹了Linux 出現(xiàn)telnet: connect to address 127.0.0.1: Connection refused錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05PXE如何實(shí)現(xiàn)自動(dòng)化部署Linux系統(tǒng)介紹
大家好,本篇文章主要講的是PXE如何實(shí)現(xiàn)自動(dòng)化部署Linux系統(tǒng)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12詳解linux下的.net/mvc/cms程序結(jié)構(gòu)
本片文章給大家詳細(xì)分析了在linux下.net/mvc/cms程序詳細(xì)結(jié)構(gòu)說(shuō)明,了解這個(gè)對(duì)在linux下程序開發(fā)非常有用,有興趣的參考學(xué)習(xí)下吧。2017-12-12sersync實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步的方法
下面小編就為大家分享一篇sersync實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)同步的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12