溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS環(huán)境中怎么部署nginx、php和虛擬主機(jī)

發(fā)布時(shí)間:2022-03-24 15:42:13 來源:億速云 閱讀:209 作者:iii 欄目:web開發(fā)

這篇“CentOS環(huán)境中怎么部署nginx、php和虛擬主機(jī)”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“CentOS環(huán)境中怎么部署nginx、php和虛擬主機(jī)”文章吧。

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)建臨時(shí)目錄

復(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
# 指定啟動(dòng)用戶:
user www www;
# 進(jìn)程數(shù)量,nginx作者認(rèn)為一個(gè)就可以,根據(jù)自己的訪問量修改
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ī)配置文件存放的目錄

復(fù)制代碼 代碼如下:

mkdir /opt/web/nginx/conf/sites

這樣配置后,需要新增加虛擬主機(jī)的直接在 nginx/conf/sites/目錄下,添加配置文件即可
例如:現(xiàn)在有 www.jb51.net 域名
建立:/opt/web/nginx/conf/sites/www.jb51.net.conf 文件
內(nèi)容如下:

復(fù)制代碼 代碼如下:

server {
listen 80;
client_max_body_size 10m;
#多個(gè)域名用空格分割,第一個(gè)為默認(rèn)
server_name www.jb51.net jb51.net;
charset utf-8;
index index.html index.htm index.php;
# 定義根目錄
set $root /var/webroot/www.jb51.net/;
# 設(shè)置站點(diǎn)路徑
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{
# 將其它的請求轉(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.jb51.net.log;
# 加載.htaccess重寫文件,注意,這里不支持變量路徑
# 不能寫成 include $root/www.jb51.net/.htaccess;
# include /var/webroot/www.jb51.net/.htaccess;
# 開啟域名跳轉(zhuǎn),則當(dāng)訪問出錯(cuò)后,其他域名會自動(dòng)跳轉(zhuǎn)到 www.jb51.net
# 注意,這里我說的是,僅僅當(dāng)訪問出錯(cuò)后,才會跳轉(zhuǎn),所以,這里并不能實(shí)現(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

如果報(bào)錯(cuò),可能是你的 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"

再次運(yùn)行:./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、啟動(dòng)php-fpm

復(fù)制代碼 代碼如下:

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

啟動(dòng)nginx

復(fù)制代碼 代碼如下:

/opt/web/nginx/sbin/nginx

9、測試一下

復(fù)制代碼 代碼如下:

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

輸入和保存

復(fù)制代碼 代碼如下:

<?php
phpinfo();
?>

10、在瀏覽器地址欄輸入:http://php.jb51.net/tz.php
成功的話,可以看到phpinfo()輸出的信息                                            

以上就是關(guān)于“CentOS環(huán)境中怎么部署nginx、php和虛擬主機(jī)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI