更新日期: 2012.11.26
此篇文章已不更新
最新內容更新請參考此篇
[Nginx yum 安裝教學-完整版 (Nginx1.2.5+PHP5.4.9+MySQL5.5.28)]
Nginx介紹
nginx(發音同engine x)是一款由俄羅斯程式設計師Igor Sysoev所開發輕量級的網頁伺服器、反向代理伺服器以及電子郵件(IMAP/POP3)代理伺服器。起初是供俄國大型的入口網站及搜尋引擎Rambler(俄語:Рамблер)使用。此軟體BSD-like協議下發行,可以在UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等作業系統中執行。
安裝環境 :
Centos 5.6
Nginx
PHP
MySQL
安裝步驟 :
添加安裝源:
[root@localhost ~]# vi /etc/yum.repos.d/alt.ru.repo
[CentALT] name=CentALT Packages for Enterprise Linux 5 – $basearch baseurl=http://centos.alt.ru/repository/centos/5/$basearch/ enabled=1 gpgcheck=0 |
啟用EPEL
i386
[root@localhost ~]# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm x86_64 [root@localhost ~]# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm |
輸入KEY
[root@localhost ~]# rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL |
yum update
[root@localhost ~]# yum -y update |
升級或安裝所需要的套件
[root@localhost ~]# LANG=C
[root@localhost ~]# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers |
安裝PHP
[root@localhost ~]# yum -y install php-fpm php-cli php-pdo php-mysql php-mcrypt php-mbstring php-gd php-tidy php-xml php-xmlrpc php-pear php-pecl-memcache php-eaccelerator php-mysql |
安裝MySQL
[root@localhost ~]# yum -y install mysql-server mysql-devel |
安裝Nginx
[root@localhost ~]# yum -y install nginx |
設定開機啟動
方法一:
[root@localhost ~]# echo “ulimit -SHn 65535″ >> /etc/rc.d/rc.local [root@localhost ~]# chkconfig –level 345 php-fpm on [root@localhost ~]# chkconfig –level 345 nginx on [root@localhost ~]# chkconfig –level 345 mysqld on
方法二: [root@localhost ~]# vi /etc/rc.d/rc.local #在最後面加入以下內容 ulimit -SHn 65535 /etc/init.d/php-fpm start /usr/local/webserver/nginx/sbin/nginx /data0/mysql/3306/mysql start 方法三: [root@localhost ~]# echo “ulimit -SHn 65535″ >> /etc/rc.d/rc.local [root@localhost ~]# echo “/etc/init.d/php-fpm start ” >> /etc/rc.d/rc.local [root@localhost ~]# echo “/usr/local/webserver/nginx/sbin/nginx” >> /etc/rc.d/rc.local [root@localhost ~]# echo “/data0/mysql/3306/mysql start” >> /etc/rc.d/rc.local |
安裝spawn-fcgi來運行php-cgi
[root@localhost ~]# yum -y install spawn-fcgi |
下載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 |
查看進程是否正常
[root@localhost ~]# netstat -tulpn | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5046/php-cgi |
默認情況下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
有關優化和設定的部份也可參考 此篇 的設定方式
1. fastcgi_params
[root@localhost ~]# vim /etc/nginx/fastcgi_params
#加入 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
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; |
3. 修改php-fpm.conf
[root@localhost ~]# vim /etc/php-fpm.conf
<value name=”max_children”>5</value> #修改為 <value name=”max_children”>20</value> |
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
#關閉不需要的表類型,若有使用到就不用加入此項 skip-innodb |
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 #加入以下內容 <? php phpinfo(); ?> |
設定完成後可以開啟網頁輸入ip或是網址,可看到下圖。
相關內容可參考Nginx安裝教學 (Nginx1.0.4+PHP5.3.6+MySQL5.5.14)
評論
此文章尚無評論。