首頁 運維 Nginx CentOS環境中怎麼部署nginx、php和虛擬主機

CentOS環境中怎麼部署nginx、php和虛擬主機

May 13, 2023 pm 12:40 PM
php centos nginx

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_proxied any;
gzip. 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;###}##### denyaccess to .htroot aches, docifum #### 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;ss#ftls. ;#### ssl_ciphers high:!anull:!md5;#### ssl_prefer_server_ciphers on;#### location / {#### root html;##### index index.html index.htm;################ 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;##profprod-#pronxy 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

#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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP的目的:構建動態網站 PHP的目的:構建動態網站 Apr 15, 2025 am 12:18 AM

PHP用於構建動態網站,其核心功能包括:1.生成動態內容,通過與數據庫對接實時生成網頁;2.處理用戶交互和表單提交,驗證輸入並響應操作;3.管理會話和用戶認證,提供個性化體驗;4.優化性能和遵循最佳實踐,提升網站效率和安全性。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

為什麼要使用PHP?解釋的優點和好處 為什麼要使用PHP?解釋的優點和好處 Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

PHP:處理數據庫和服務器端邏輯 PHP:處理數據庫和服務器端邏輯 Apr 15, 2025 am 12:15 AM

PHP在數據庫操作和服務器端邏輯處理中使用MySQLi和PDO擴展進行數據庫交互,並通過會話管理等功能處理服務器端邏輯。 1)使用MySQLi或PDO連接數據庫,執行SQL查詢。 2)通過會話管理等功能處理HTTP請求和用戶狀態。 3)使用事務確保數據庫操作的原子性。 4)防止SQL注入,使用異常處理和關閉連接來調試。 5)通過索引和緩存優化性能,編寫可讀性高的代碼並進行錯誤處理。

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

centos關機命令行 centos關機命令行 Apr 14, 2025 pm 09:12 PM

CentOS 關機命令為 shutdown,語法為 shutdown [選項] 時間 [信息]。選項包括:-h 立即停止系統;-P 關機後關電源;-r 重新啟動;-t 等待時間。時間可指定為立即 (now)、分鐘數 ( minutes) 或特定時間 (hh:mm)。可添加信息在系統消息中顯示。

See all articles