3.3下載spawn-fcgi 啟動腳本
[root@localhost ~]# cd /etc/init.d/
[root@localhost ~]# wget http://blog.faq-book.com/download/php_cgi
[root@localhost ~]# chmod +x /etc/init.d/php_cgi
[root@localhost ~]# /etc/init.d/php_cgi start
3.4 查看進程是否正常
[root@localhost ~]# netstat -tulpn | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5046/php-cgi
3.5 默認情況下PHP服務器監聽127.0.0.1:9000端口
[root@localhost ~]# vi /etc/nginx/nginx.conf
#修改如下(把#去調)
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
優化Nginx
4.1 fastcgi_params
[root@localhost ~]# vim /etc/nginx/fastcgi_params
#加入
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
4.2 修改nginx.conf,(以下只針對重要和要修改的內容做說明)
[root@localhost ~]# vi /etc/nginx/nginx.conf
user nginx;
worker_processes 10; #根據CPU和記憶體配置,設置2到10都OK
worker_rlimit_nofile 100000; #啟用epoll
error_log /var/log/nginx/error.log; #error log的存放路徑
pid /var/run/nginx.pid; #pid的路徑
access_log /var/log/nginx/access.log main; # access_log的路徑
server {
limit_conn myzone 10;
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php; #加入index.php才讀的到php檔
}
#以下是一些參數的調整和優化,可自行決定是否要增加或修改
#原本的設定為:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
gzip on;
gzip_static on;
gzip_comp_level 5;
gzip_min_length 1024;
keepalive_timeout 65;
limit_zone myzone $binary_remote_addr 10m;
#修改為:
#參數調整:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;
keepalive_timeout 50;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
#Fastcgi優化:
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
#開啟gzip並優化:
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
4.3修改php-fpm.conf
[root@localhost ~]# vim /etc/php-fpm.conf
<value name="max_children">5</value>
#修改為
<value name="max_children">20</value>
4.4 MySQL調整
修改my.conf
[root@localhost ~]# vim /etc/my.conf
#可依照自行的需求,在 [mysqld]內加入以下內容
skip-network
skip-innodb
#取消文件系統的外部鎖
skip-locking
skip-bdb
#不進行域名反解析,要注意帶來的權限問題
skip-name-resolve
#索引暫存,根據記憶體大小而定。如果是獨立的DB伺服器,可以設定最高50%的記憶體總量。
但如果是小記憶體的VPS,特別是Openvz的vps,建議設定為記憶體大小的25%M
key_buffer = 512M
#連接列表總數
back_log = 200
max_allowed_packet = 3M
#緩衝可從用的排程數
thread_cache = 128
thread_cache_size = 8
thread_stack = 64K
#打開暫存總數,可避免頻繁開啟數據列表產生的問題
table_cache = 512
#每個排程所需要的緩衝
sort_buffer_size = 4M
#每個排程讀取索引所需要的緩衝
read_buffer_size = 4M
#MyISAM表發生變化時重新排序所需要的緩衝
myisam_sort_buffer_size = 64M
#系統內核調整(查詢結果暫存)
query_cache_size = 128M
query_cache_limit = 1M
#設定時間斷線,必勉長時間連線
set-variable = wait_timeout=60
#最大開發數,CPU數量*2
thread_concurrency = 4
#記錄慢查詢,對慢查詢一一優化
log-slow-queries = slow.log
long_query_time = 1
#關閉不需要的表類型,若有使用到就不用加入此項
skip-innodb
skip-bdb
4.5 修改sysctl.conf
[root@localhost ~]# vim /etc/sysctl.conf
#按Ctrl+g到最後一行加入以下內容
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
[root@localhost ~]# /sbin/sysctl –p #使設定立即生效
自行檢測看所需要套件是否都有安裝完成
到nginx.conf所設定的目錄中,設定一個phpinfo。就可查詢是否有安裝成功。
設定方式如下
[root@localhost ~]# cd /usr/share/nginx/html
[root@localhost ~]# vi index.php
#加入以下內容
設定完成後可以開啟網頁輸入ip或是網址,可看到下圖。
Nginx yum安裝教學(Nginx+PHP+MySQL)(上)
全文同步於FAQ-BOOK
IT鐵人文章分享