Nginx 介紹
Nginx 是俄羅斯人所編寫,是一款高效能的 HTTP 和反向代理服務器。也是 IMAP/POP3/SMTP 代理服務器。Nginx (發音是engine x)由俄羅斯的程式設計師 Igor Sysoev所開發的。可安裝在 UNIX、GNU/Linux、FreeBSD、Mac OS X、 Solaris 以及 Microsoft Windows等操作系統中。在 NETCRAFT 2012年8月 WEB Server 調查中,使用 Nginx 當服務器或是代理服務器就佔全世界的11.48%。隨著 Nginx 在很多大型網站的廣泛使用,其穩定、高效的特性逐漸被越來越多的使用者認可。
Nginx 和 Apache 相比較下有以下優勢 :
性能上 : Nginx 使用較少的系統資源,能支持更多的開發,已達到更高的效能。
功能上 : Nginx 是優良的代理服務器和負載平衡服務器。
安裝上 : Nginx 安裝簡單且配置靈活。
更多介紹可參考 :
安裝環境 :
在此篇教學中所用到的所有套件,如果提供者載點死掉。都可在本站的載點自行下載使用
Centos 5.8
Nginx 1.2.5
PHP 5.4.9
MySQL 5.5.28
安裝方式 :
安裝和更新常用套件
更新已有的套件
先把已安裝的套件更新一下。若不想更新已有的套件的話就略過
1 |
[root@localhost ~]# yum -y update |
安裝常用套件
1 2 |
[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 patch zip bison |
編譯 安裝 MySQL 5.5.28
安装cmake
1 2 3 4 5 6 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://www.cmake.org/files/v2.8/cmake-2.8.10.tar.gz [root@localhost ~]# tar -zxvpf cmake-2.8.10.tar.gz [root@localhost ~]# cd cmake-2.8.10 [root@localhost ~]# ./bootstrap --prefix=/usr/local/cmake [root@localhost ~]# gmake && gmake install |
修改 centos環境變數
1 2 3 4 5 |
[root@localhost ~]# vim /etc/profile 找到export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC 下一行加入 export PATH=/usr/local/cmake/bin:$PATH 儲存後重啟系統 |
編譯 安裝 MySQL 5.5.28
1. 建立使用者和群組
1 2 |
[root@localhost ~]# /usr/sbin/groupadd mysql [root@localhost ~]# /usr/sbin/useradd -g mysql mysql |
2. 編譯安裝 MySQL 以及修改權限
1 2 3 4 5 6 7 8 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://download.faq-book.com/download/System/Linux/Nginx/mysql-5.5.28.tar.gz [root@localhost ~]# tar -zxvpf mysql-5.5.28.tar.gz [root@localhost ~]# cd mysql-5.5.28 [root@localhost ~]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql/ [root@localhost ~]# make && make install [root@localhost ~]# chmod +w /usr/local/webserver/mysql [root@localhost ~]# chown -R mysql:mysql /usr/local/webserver/mysql |
建立 MySQL 存放目錄
MySQL 存放目錄位置可自行修改,不過若有修改請注意後續設定的路徑
1 2 3 4 |
[root@localhost ~]# mkdir -p /data/mysql/3306/data/ [root@localhost ~]# mkdir -p /data/mysql/3306/binlog/ [root@localhost ~]# mkdir -p /data/mysql/3306/relaylog/ [root@localhost ~]# chown -R mysql:mysql /data/mysql/ |
建立資料表
用MySQL用戶帳號的身份來建立資料表
1 |
[root@localhost ~]# /usr/local/webserver/mysql/scripts/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/data/mysql/3306/data --user=mysql |
建立my.cnf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
[root@localhost ~]# vi /data/mysql/3306/my.cnf #輸入以下內容: [client] character-set-server = utf8 port = 3306 socket = /tmp/mysql.sock [mysqld] character-set-server = utf8 replicate-ignore-db = mysql replicate-ignore-db = test replicate-ignore-db = information_schema user = mysql port = 3306 socket = /tmp/mysql.sock basedir = /usr/local/webserver/mysql datadir = /data/mysql/3306/data log-error = /data/mysql/3306/mysql_error.log pid-file = /data/mysql/3306/mysql.pid open_files_limit = 10240 back_log = 600 max_connections = 5000 max_connect_errors = 6000 table_cache = 614 external-locking = FALSE max_allowed_packet = 32M sort_buffer_size = 1M join_buffer_size = 1M thread_cache_size = 300 #thread_concurrency = 8 query_cache_size = 512M query_cache_limit = 2M query_cache_min_res_unit = 2k default-storage-engine = MyISAM thread_stack = 192K transaction_isolation = READ-COMMITTED tmp_table_size = 246M max_heap_table_size = 246M long_query_time = 3 log-slave-updates log-bin = /data/mysql/3306/binlog/binlog binlog_cache_size = 4M binlog_format = MIXED max_binlog_cache_size = 8M max_binlog_size = 1G relay-log-index = /data/mysql/3306/relaylog/relaylog relay-log-info-file = /data/mysql/3306/relaylog/relaylog relay-log = /data/mysql/3306/relaylog/relaylog expire_logs_days = 30 key_buffer_size = 256M read_buffer_size = 1M read_rnd_buffer_size = 16M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover interactive_timeout = 120 wait_timeout = 120 skip-name-resolve #master-connect-retry = 10 slave-skip-errors = 1032,1062,126,1114,1146,1048,1396 #master-host = 192.168.1.2 #master-user = username #master-password = password #master-port = 3306 server-id = 1 innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 512M innodb_data_file_path = ibdata1:256M:autoextend innodb_file_io_threads = 4 innodb_thread_concurrency = 8 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 16M innodb_log_file_size = 128M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 innodb_file_per_table = 0 #log-slow-queries = /data/mysql/3306/slow.log #long_query_time = 10 [mysqldump] quick max_allowed_packet = 32M |
建立 MySQL 開啟和關閉的 Shell
此shell的用途為管理MySQL的開啟和關閉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
[root@localhost ~]# vi /data/mysql/3306/mysql #輸入以下內容 : #!/bin/sh mysql_port=3306 mysql_username="admin" mysql_password="12345678" function_start_mysql() { printf "Starting MySQL...\n" /bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/data/mysql/{mysql_port}/my.cnf 2>&1 > /dev/null & } function_stop_mysql() { printf "Stoping MySQL...\n" /usr/local/webserver/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown } function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql sleep 5 function_start_mysql } function_kill_mysql() { kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}') kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}') } if [ "$1" = "start" ]; then function_start_mysql elif [ "$1" = "stop" ]; then function_stop_mysql elif [ "$1" = "restart" ]; then function_restart_mysql elif [ "$1" = "kill" ]; then function_kill_mysql else printf "Usage: /data/mysql/{mysql_port}/mysql {start|stop|restart|kill}\n" fi |
給shell可執行的權限
1 |
[root@localhost ~]# chmod +x /data/mysql/3306/mysql |
建立root權限的用戶
1. 啟動MySQL
1 |
[root@localhost ~]# /data/mysql/3306/mysql start |
2. 登入MySQL
尚未設定相關帳密,此部份先按Enter即可
1 |
[root@localhost ~]# /usr/local/webserver/mysql/bin/mysql -u root -p -S /tmp/mysql.sock |
3. 建立具有root權限的用戶
帳號:admin、密碼:12345678。帳密部份可自行修改,但若有修改的話vi /data/mysql/3306/mysql也須要一起修改
1 2 3 4 |
mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678'; mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678'; mysql> quit Bye |
4. 停用MySQL
1 |
[root@localhost ~]# /data/mysql/3306/mysql stop |
設定軟連結
1 |
[root@localhost ~]# ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/local/lib |
MySQL啟動和停用操作
1 2 |
[root@localhost ~]# /data/mysql/3306/mysql start # 啟用MySQL [root@localhost ~]# /data/mysql/3306/mysql stop # 停用 MySQL |
安装PHP 5.4.9相關模組
編譯安裝PHP5.4.9的相關模組
libiconv-1.14
1 2 3 4 5 6 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz [root@localhost ~]# tar -zxvpf libiconv-1.14.tar.gz [root@localhost ~]# cd libiconv-1.14 [root@localhost ~]# ./configure --prefix=/usr/local [root@localhost ~]# make && make install |
libmcrypt-2.5.8
1 2 3 4 5 6 7 8 9 10 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget ftp://ftp.eenet.ee/pub/FreeBSD/distfiles/libmcrypt-2.5.8.tar.gz [root@localhost ~]# tar -zxvpf libmcrypt-2.5.8.tar.gz [root@localhost ~]# cd libmcrypt-2.5.8 [root@localhost ~]# ./configure [root@localhost ~]# make && make install [root@localhost ~]# /sbin/ldconfig [root@localhost ~]# cd libltdl/ [root@localhost ~]# ./configure --enable-ltdl-install [root@localhost ~]# make && make install |
mhash-0.9.9.9
1 2 3 4 5 6 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download [root@localhost ~]# tar -zxvpf mhash-0.9.9.9.tar.gz [root@localhost ~]# cd mhash-0.9.9.9 [root@localhost ~]# ./configure --prefix=/usr/local [root@localhost ~]# make && make install |
mcrypt-2.6.8
1 2 3 4 5 6 7 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download [root@localhost ~]# tar -zxvpf mcrypt-2.6.8.tar.gz [root@localhost ~]# cd mcrypt-2.6.8 [root@localhost ~]# /sbin/ldconfig [root@localhost ~]# LD_LIBRARY_PATH=/usr/local/lib ./configure [root@localhost ~]# make && make install |
設定軟連結
1 2 3 4 5 6 7 8 9 10 |
[root@localhost ~]# ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la [root@localhost ~]# ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so [root@localhost ~]# ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 [root@localhost ~]# ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 [root@localhost ~]# ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a [root@localhost ~]# ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la [root@localhost ~]# ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so [root@localhost ~]# ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 [root@localhost ~]# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 [root@localhost ~]# ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config |
編譯安裝PHP (FastCGI模式)
編譯 安裝PHP
1 2 3 4 5 6 7 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://download.faq-book.com/download/System/Linux/Nginx/php-5.4.9.tar.gz [root@localhost ~]# tar -zxvpf php-5.4.9.tar.gz [root@localhost ~]# cd php-5.4.9 [root@localhost ~]# ./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local/lib --with-freetype-dir=/usr/local/lib --with-jpeg-dir=/usr/local/lib --with-png-dir=/usr/local/lib --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap [root@localhost ~]# make ZEND_EXTRA_LIBS='-liconv' [root@localhost ~]# make install [root@localhost ~]# cp /etc/php.ini /usr/local/webserver/php/etc/ |
安裝php-fpm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@localhost ~]# useradd php-fpm [root@localhost ~]# cd /opt/php-5.4.9 #此為php-5.3.6的下載存放路徑 [root@localhost ~]# cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost ~]# chmod 755 /etc/init.d/php-fpm [root@localhost ~]# cp -f sapi/fpm/php-fpm.conf.in /usr/local/webserver/php/etc/php-fpm.conf [root@localhost ~]# vim /usr/local/webserver/php/etc/php-fpm.conf #請更換為以下內容 pid = /usr/local/webserver/php/var/run/php-fpm.pid listen = 127.0.0.1:9000 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.min_spare_servers = 3 pm.max_spare_servers = 35 pm.start_servers = 20 |
php-fpm啟用和停用操作
1 2 3 4 5 |
[root@localhost ~]# /sbin/chkconfig --add php-fpm #加入到開機服務列表 [root@localhost ~]# /sbin/chkconfig php-fpm on #設定開機自動開啟 [root@localhost ~]# /etc/init.d/php-fpm start #啟動(或使用service php-fpm start) [root@localhost ~]# /etc/init.d/php-fpm stop #停止(或使用service php-fpm stop) [root@localhost ~]# /etc/init.d/php-fpm reload #重啟(或使用service php-fpm reload) |
安裝 PHP FastCGI模式的擴展模組
memcache-2.2.7
當前穩定版的是2.2.7版
1 2 3 4 5 6 7 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://pecl.php.net/get/memcache-2.2.7.tgz [root@localhost ~]# tar -zxvpf memcache-2.2.7.tgz [root@localhost ~]# cd memcache-2.2.7 [root@localhost ~]# /usr/local/webserver/php/bin/phpize [root@localhost ~]# ./configure --with-php-config=/usr/local/webserver/php/bin/php-config [root@localhost ~]# make && make install |
eaccelerator
eaccelerator 為開源碼,是 PHP 的加速器,對動態內容和 PHP腳本的暫存優化,使其程式碼執行效率提高1-10倍。
1 2 3 4 5 6 7 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://download.faq-book.com/download/System/Linux/Nginx/eaccelerator-eaccelerator-42067ac.tar.gz [root@localhost ~]# tar -zxvpf eaccelerator-eaccelerator-42067ac.tar.gz [root@localhost ~]# cd eaccelerator-eaccelerator-42067ac [root@localhost ~]# /usr/local/webserver/php/bin/phpize [root@localhost ~]# ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config [root@localhost ~]# make && make install |
PDO_MYSQL-1.0.2
1 2 3 4 5 6 7 8 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz [root@localhost ~]# tar -zxvpf PDO_MYSQL-1.0.2.tgz [root@localhost ~]# cd PDO_MYSQL-1.0.2 [root@localhost ~]# ln -s /usr/local/webserver/mysql/include/* /usr/local/include/ [root@localhost ~]# /usr/local/webserver/php/bin/phpize [root@localhost ~]# ./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql [root@localhost ~]# make && make install |
ImageMagick
在設定ImageMagick模組時,要先確定系統時間是否正確。
1 2 3 4 5 6 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget ftp://mirror.aarnet.edu.au/pub/imagemagick/ImageMagick.tar.gz [root@localhost ~]# tar -zxvpf ImageMagick.tar.gz [root@localhost ~]# cd ImageMagick-6.8.0-7 [root@localhost ~]# ./configure [root@localhost ~]# make && make install |
imagick-3.1.0RC2
此部份要注意一下,由於所安裝的PHP版本較新,所以在imagick的部份要安裝最新版本才不會有問題
1 2 3 4 5 6 7 8 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://pecl.php.net/get/imagick-3.1.0RC2.tgz [root@localhost ~]# tar -zxvpf imagick-3.1.0RC2.tgz [root@localhost ~]# cd imagick-3.1.0RC2 [root@localhost ~]# /usr/local/webserver/php/bin/phpize [root@localhost ~]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig [root@localhost ~]# ./configure --with-php-config=/usr/local/webserver/php/bin/php-config [root@localhost ~]# make && make install |
修改php.ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@localhost ~]# vim /usr/local/webserver/php/etc/php.ini # 先找到 extension_dir = "/usr/lib64/php/modules" # 修改為 extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/" # 然後在下方加入 extension = "memcache.so" extension = "pdo_mysql.so" extension = "imagick.so" # 再來找到 output_buffering = 4096 # 修改為 output_buffering = on |
若是不想手動修改可下以下指令
1 2 3 4 |
[root@localhost ~]# sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\n#' /usr/local/webserver/php/etc/php.ini [root@localhost ~]# sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/webserver/php/etc/php.ini [root@localhost ~]# sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /usr/local/webserver/php/etc/php.ini [root@localhost ~]# sed -i "s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g" /usr/local/webserver/php/etc/php.ini |
配置eaccelerator 加速PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@localhost ~]# mkdir -p /usr/local/webserver/eaccelerator_cache [root@localhost ~]# vim /usr/local/webserver/php/etc/php.ini #到文件最後面加上以下資料(可按shift+g到最後) [eaccelerator] zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so" eaccelerator.shm_size="64" eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="3600" eaccelerator.shm_prune_period="3600" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9" |
建立使用者及目錄
建立www使用者和群組,以及建立網頁資料存放目錄
1 2 3 4 5 |
[root@localhost ~]# /usr/sbin/groupadd www [root@localhost ~]# /usr/sbin/useradd -g www www [root@localhost ~]# mkdir -p /var/www/blog [root@localhost ~]# chmod +w /var/www/blog [root@localhost ~]# chown -R www:www /var/www/blog |
安裝Nginx
安裝Nginx所需要的pcre
1 2 3 4 5 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://ftp.exim.llorien.org/pcre/pcre-8.31.tar.gz [root@localhost ~]# tar -zxvpf pcre-8.31.tar.gz [root@localhost ~]# cd pcre-8.31 [root@localhost ~]# ./configure [root@localhost ~]# make && make install |
安裝Nginx
1 2 3 4 5 6 |
[root@localhost ~]# cd /opt [root@localhost ~]# wget http://nginx.org/download/nginx-1.2.5.tar.gz [root@localhost ~]# tar -zxvpf nginx-1.2.5.tar.gz [root@localhost ~]# cd nginx-1.2.5 [root@localhost ~]# ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module [root@localhost ~]# make && make install |
建立Nginx log目錄
1 2 3 |
[root@localhost ~]# mkdir -p /data/logs [root@localhost ~]# chmod +w /data/logs [root@localhost ~]# chown -R www:www /data/logs |
建立軟連結
1 |
[root@localhost ~]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1 |
建立nginx.conf
在/usr/local/webserver/nginx/conf/ 目錄中重新建立一個新的nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
[root@localhost ~]# mv /usr/local/webserver/nginx/conf/nginx.conf /usr/local/webserver/nginx/conf/nginx.conf.bak [root@localhost ~]# vim /usr/local/webserver/nginx/conf/nginx.conf #加入以下內容: user www www; worker_processes 10; # 根據CPU和記憶體配置,設置2到10都OK error_log /data/logs/nginx_error.log crit; # error log的存放路徑 pid /usr/local/webserver/nginx/nginx.pid; # pid的路徑 events { use epoll; # 啟用epoll worker_connections 1024; } http { 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 /data/logs/access.log main; sendfile on; keepalive_timeout 65; # 連線timeout秒數 server { listen 80; server_name localhost; root /usr/local/webserver/nginx/html; index index.html index.htm index.php; location ~\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/webserver/nginx/html$fastcgi_script_name; include /usr/local/webserver/nginx/conf/fastcgi.conf; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } |
Nginx 常用操作
測試
如果有做過修改的話可用以下指定測試修改後是否有問題
1 2 3 4 |
[root@localhost ~]# /usr/local/webserver/nginx/sbin/nginx -t #如果測試後結果如下 就表示修改沒問題 nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful |
reload
在不停止Nginx服務情況下,重啟Nginx
1 |
[root@localhost ~]# /usr/local/webserver/nginx/sbin/nginx -s reload |
開啟和停止
1 2 3 |
[root@localhost ~]# ulimit -SHn 65535 [root@localhost ~]# /usr/local/webserver/nginx/sbin/nginx #開啟 [root@localhost ~]# /usr/local/webserver/nginx/sbin/nginx -s stop #停止 |
檢查是否正常
開網頁查看
在網址的地方輸入「http://ip或domain」。如果有看到出現「Welcome to nginx !」的字樣的話就表示安裝成功。接下來就是一些細部調整。
查看進程是否正常
1 2 3 4 5 6 |
[root@localhost ~]# netstat -tulpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7742/php-fpm tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 8039/mysqld tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7757/nginx |
查看phpinfo.php
到nginx.conf 所設定的 Web 目錄中,設定 phpinfo.php。可查詢相關套件是否有安裝成功。
設定方式如下
1 2 3 4 5 |
[root@localhost ~]# vim /usr/local/webserver/nginx/html/phpinfo.php #加入以下內容 <?php phpinfo(); ?> |
在都設定好後,可開啟網頁在網址的地方輸入「http://ip/phpinfo.php」就可看到以下內容
設定開機後自動啟動 Nginx、php-fpm和 MqSQL
1 2 3 4 5 6 |
[root@localhost ~]# vi /etc/rc.d/rc.local #在最後面加入以下內容 ulimit -SHn 65535 /etc/init.d/php-fpm start /usr/local/webserver/nginx/sbin/nginx /data/mysql/3306/mysql start |
或是直接輸入
1 2 3 4 |
[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 "/data/mysql/3306/mysql start" >> /etc/rc.d/rc.local |
優化設定
nginx.conf參數說明
以下是一些參數的調整和優化,可自行決定是否要增加或修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#參數調整: 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; |
優化Linux內核參數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@localhost ~]# vi /etc/sysctl.conf #到最後面加入以下內容 : # 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 log 的腳本
建立新腳本
1 2 3 4 5 6 7 8 9 10 11 |
[root@localhost ~]# vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh #輸入以下內容 #!/bin/bash # This script run at 00:00 # The Nginx logs path logs_path="/usr/local/webserver/nginx/logs/" mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/ mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid` |
設定crontab 時間可以自設
1 2 3 |
[root@localhost ~]# crontab -e #加入以下內容 00 00 * * * /bin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh |
Nginx 的安裝到這邊就大致完成
參考資料:
http://blog.s135.com/nginx_php_v6/
http://wiki.nginx.org/NginxChs
太讚了, 我都一直在玩 lnmp, 有空可以切磋一下 ^_^
http://fb.me/afgnsu
這是我的榮幸