首頁 > 運維 > Nginx > 主體

CentOS環境中如何部署nginx、php和虛擬主機配置

王林
發布: 2023-05-20 14:25:06
轉載
1264 人瀏覽過

部署時間: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 on;
gzip_min_length 1000;
gzip. /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;##1
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 fast#2; 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 somenamename somename:8080;
## 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;
#loroot#"
"#n#"
#"#log#" 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;##proxyr;# ;
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 ^/(.*)$ //www.jb51.net/$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{
# 將其它的請求轉交給uwsgi
include uwsgi_params;
uwsgi_pass unix:/tmp/360ito_uwsgi.sock;
proxy_set_header x-real-iphostremote_addr;






##如果 好吧 好吧 好吧D 她 她 她 她 她 hostr;
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.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/

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##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.jb51.net/tz.php######輸入並儲存########複製程式碼 程式碼如下:##########

10、在瀏覽器網址列輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的資訊                     

以上是CentOS環境中如何部署nginx、php和虛擬主機配置的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板