LNMP安裝與配置

簡介

Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,它已經在該站點運行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發佈。儘管還是測試版,但是,Nginx 已經因爲它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名了。

Nginx優點
Nginx支持高併發連接,居官方測試Nginx支持5萬併發,採用了最新的epoll網絡I/o模型,Nginx相當的穩定、功能豐富、安裝配置簡單、低系統資源,這麼好的用軟件,下面就來玩玩吧

下面通過簡單實例,理解nginx:
一、LNMP編譯安裝的過程
二、配置基於域名的虛擬主機
三、開啓監控nginx服務運行狀態,並訪問時基於用戶的認證
四、啓用基於ssl的認證功能
五、安裝Discuz論壇




一、LNMP編譯安裝的過程
 

1、編譯安裝Nginx

  1. 解決依賴關係  
  2. #yum -y install " Development Tools"  
  3. #yum -y install "Development Libraries"  
  4. #yum -y install pcre-devel  
  5.  
  6. 添加nginx用戶  
  7. #groupadd -r nginx   
  8. #useradd -r -g nginx -s /sbin/nologin nginx  
  9.  
  10. 編譯安裝nginx  
  11. # ./configure \  
  12.   --prefix=/usr \                                 
  13.   --sbin-path=/usr/sbin/nginx \  
  14.   --conf-path=/etc/nginx/nginx.conf \  
  15.   --error-log-path=/var/log/nginx/error.log \  
  16.   --http-log-path=/var/log/nginx/access.log \  
  17.   --pid-path=/var/run/nginx/nginx.pid  \  
  18.   --lock-path=/var/lock/nginx.lock \  
  19.   --user=nginx \  
  20.   --group=nginx \  
  21.   --with-http_ssl_module \  
  22.   --with-http_flv_module \  
  23.   --with-http_stub_status_module \  
  24.   --with-http_gzip_static_module \  
  25.   --http-client-body-temp-path=/var/tmp/nginx/client/ \  
  26.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \  
  27.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \  
  28.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \  
  29.   --http-scgi-temp-path=/var/tmp/nginx/scgi \  
  30.   --with-pcre  
  31. # make && make install  
  32.  
  33.  
  34. 爲nginx提供SysV init腳本:  
  35.  
  36. 新建文件/etc/rc.d/init.d/nginx,內容如下:  
  37. #!/bin/sh  
  38. #  
  39. # nginx - this script starts and stops the nginx daemon  
  40. #  
  41. # chkconfig:   - 85 15   
  42. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \  
  43. #               proxy and IMAP/POP3 proxy server  
  44. # processname: nginx  
  45. # config:      /etc/nginx/nginx.conf  
  46. # config:      /etc/sysconfig/nginx  
  47. # pidfile:     /var/run/nginx.pid  
  48.    
  49. # Source function library.  
  50. . /etc/rc.d/init.d/functions  
  51.    
  52. # Source networking configuration.  
  53. . /etc/sysconfig/network  
  54.    
  55. # Check that networking is up.  
  56. [ "$NETWORKING" = "no" ] && exit 0  
  57.    
  58. nginx="/usr/sbin/nginx" 
  59. prog=$(basename $nginx)  
  60.    
  61. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  62.    
  63. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  64.    
  65. lockfile=/var/lock/subsys/nginx  
  66.    
  67. make_dirs() {  
  68.    # make required directories  
  69.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`  
  70.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`  
  71.    for opt in $options; do  
  72.        if [ `echo $opt | grep '.*-temp-path'` ]; then  
  73.            value=`echo $opt | cut -d "=" -f 2`  
  74.            if [ ! -d "$value" ]; then  
  75.                # echo "creating" $value  
  76.                mkdir -p $value && chown -R $user $value  
  77.            fi  
  78.        fi  
  79.    done  
  80. }  
  81.    
  82. start() {  
  83.     [ -x $nginx ] || exit 5  
  84.     [ -f $NGINX_CONF_FILE ] || exit 6  
  85.     make_dirs  
  86.     echo -n $"Starting $prog: "  
  87.     daemon $nginx -c $NGINX_CONF_FILE  
  88.     retval=$?  
  89.     echo  
  90.     [ $retval -eq 0 ] && touch $lockfile  
  91.     return $retval  
  92. }  
  93.    
  94. stop() {  
  95.     echo -n $"Stopping $prog: "  
  96.     killproc $prog -QUIT  
  97.     retval=$?  
  98.     echo  
  99.     [ $retval -eq 0 ] && rm -f $lockfile  
  100.     return $retval  
  101. }  
  102.    
  103. restart() {  
  104.     configtest || return $?  
  105.     stop  
  106.     sleep 1  
  107.     start  
  108. }  
  109.    
  110. reload() {  
  111.     configtest || return $?  
  112.     echo -n $"Reloading $prog: "  
  113.     killproc $nginx -HUP  
  114.     RETVAL=$?  
  115.     echo  
  116. }  
  117.    
  118. force_reload() {  
  119.     restart  
  120. }  
  121.    
  122. configtest() {  
  123.   $nginx -t -c $NGINX_CONF_FILE  
  124. }  
  125.    
  126. rh_status() {  
  127.     status $prog  
  128. }  
  129.    
  130. rh_status_q() {  
  131.     rh_status >/dev/null 2>&1  
  132. }  
  133.    
  134. case "$1" in  
  135.     start)  
  136.         rh_status_q && exit 0  
  137.         $1  
  138.         ;;  
  139.     stop)  
  140.         rh_status_q || exit 0  
  141.         $1  
  142.         ;;  
  143.     restart|configtest)  
  144.         $1  
  145.         ;;  
  146.     reload)  
  147.         rh_status_q || exit 7  
  148.         $1  
  149.         ;;  
  150.     force-reload)  
  151.         force_reload  
  152.         ;;  
  153.     status)  
  154.         rh_status  
  155.         ;;  
  156.     condrestart|try-restart)  
  157.         rh_status_q || exit 0  
  158.             ;;  
  159.     *)  
  160.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"  
  161.         exit 2  
  162. esac  
  163.  
  164. 爲此腳本賦予執行權限:  
  165. #chmod +x /etc/rc.d/init.d/nginx  
  166.  
  167. 添加服務列表,並設置開機自動啓動  
  168. #chkconfig --add nginx  
  169. #chkconfig nginx on  
  170.  
  171. 最後啓動nginx  
  172. #/etc/init.d/nginx start  
  173.  
  174. 查看下80端口  
  175. #netstat -tlnp |grep 80 

2、編譯安裝mysql

  1. 注意:在mysql5.5以後不能使用make了,必須要使用cmake  
  2. 編譯安裝cmake  
  3. #tar xf cmake-2.8.8.tar.gz   
  4. #cd cmake-2.8.8  
  5. #./bootstrap   
  6. #make && make install  
  7.  
  8. 編譯安裝mysql  
  9. # groupadd -r mysql  
  10. # useradd -g mysql -r -d /data/mysql mysql  
  11. # tar xf mysql-5.5.25a.tar.gz   
  12. # cd mysql-5.5.25a  
  13. # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \  
  14.           -DMYSQL_DATADIR=/data/mysql \  
  15.           -DSYSCONFDIR=/etc \  
  16.           -DWITH_INNOBASE_STORAGE_ENGINE=1 \  
  17.           -DWITH_ARCHIVE_STORAGE_ENGINE=1 \  
  18.           -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \  
  19.           -DWITH_READLINE=1 \  
  20.           -DWITH_SSL=system \  
  21.           -DWITH_ZLIB=system \  
  22.           -DWITH_LIBWRAP=0 \  
  23.           -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \  
  24.           -DMYSQL_USER=mysql \  
  25.           -DDEFAULT_CHARSET=utf8 \  
  26.           -DDEFAULT_COLLATION=utf8_general_ci 
  27. # make   
  28. # make install
  29.  
  30. 編輯my.cnf文件
  31. #vim /etc/my.cnf 添加以下內容
  32. #datadir=/mydata/data  
  33.  
  34. 建立mysql數據存放的目錄  
  35. #mkdir -pv /mydata/data  
  36. #chown -R mysql.mysql /mydata/data/  
  37.  
  38. 修改屬主和組並初始化mysql  
  39. #cd /usr/local/mysql  
  40. #chown -R mysql:mysql .   
  41. #/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data  
  42. #chown -R root.mysql .  
  43.  
  44.  
  45. 爲mysql提供sysv服務腳本  
  46. #cd /usr/local/mysql   
  47. #cp support-files/mysql.server  /etc/rc.d/init.d/mysqld   
  48. #chmod +x /etc/rc.d/init.d/mysqld   
  49.  
  50. 添加至服務列表  
  51. #chkconfig --add mysqld   
  52. #chkconfig mysqld on   
  53.  
  54. 輸出mysql的man手冊至man命令的查找路徑:  
  55. #vim /etc/man.config  
  56. 添加以下內容  
  57. MANPATH  /usr/local/mysql/man  
  58.  
  59. 輸出mysql的頭文件至系統頭文件路徑/usr/include:  
  60. 這可以通過簡單的創建鏈接實現  
  61. #ln -sv /usr/local/mysql/include  /usr/include/mysql  
  62.  
  63. 輸出mysql的庫文件給系統庫查找路徑  
  64. #echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf  
  65.  
  66. 而後讓系統重新載入系統庫:  
  67. #ldconfig  
  68.  
  69. 啓動mysql服務  
  70. #service mysqld start 

3、編譯安裝php

  1. #tar xf php-5.4.4.tar.bz2   
  2. #cd php-5.4.4  
  3. #./configure --prefix=/usr/local/php \  
  4.         --with-mysql=/usr/local/mysql \  
  5.         --with-openssl \  
  6.         --enable-fpm \  
  7.         --enable-sockets \  
  8.         --enable-sysvshm  \  
  9.         --with-mysqli=/usr/local/mysql/bin/mysql_config \  
  10.         --enable-mbstring \  
  11.         --with-freetype-dir \  
  12.         --with-jpeg-dir \  
  13.         --with-png-dir \  
  14.         --with-zlib-dir \  
  15.         --with-libxml-dir=/usr \  
  16.         --enable-xml  \  
  17.         --with-mhash  \  
  18.         --with-config-file-path=/etc \  
  19.         --with-config-file-scan-dir=/etc/php.d \  
  20.         --with-bz2 \  
  21.         --with-curl \  
  22. #make && make install  
  23.  
  24. 爲php提供配置文件  
  25. #cd /root/php-5.4.4  
  26. #cp php.ini-production /etc/php.ini  
  27.  
  28. 爲php-fpm提供Sysv init腳本,並將其添加至服務列表  
  29. #cd /root/php-5.4.4  
  30. #cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm  
  31. #chmod +x /etc/rc.d/init.d/php-fpm  
  32. #chkconfig --add php-fpm  
  33. #chkconfig php-fpm on  
  34.  
  35. 爲php-fpm提供配置文件:  
  36. #cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf   
  37.  
  38. 編輯php-fpm的配置文件:  
  39. # vim /usr/local/php/etc/php-fpm.conf  
  40. 啓用以下內容  
  41. pid = /usr/local/php/var/run/php-fpm.pid   
  42.  
  43. 啓動php-fpm服務,並查看9000端口  
  44. #service php-fpm start  
  45. #netstat -tlnp |grep 9000  
  46.  
  47.  
  48. 整合nginx和php5  
  49. 編輯/etc/nginx/nginx.conf,啓用如下選項:  
  50. location ~ \.php$ {  
  51.             root           html;  
  52.             fastcgi_pass   127.0.0.1:9000;  
  53.             fastcgi_index  index.php;  
  54.             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  55.             include        fastcgi_params;  
  56.         }  
  57.  
  58. 編輯/etc/nginx/fastcgi_params,將其內容更改爲如下內容:  
  59. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;  
  60. fastcgi_param  SERVER_SOFTWARE    nginx;  
  61. fastcgi_param  QUERY_STRING       $query_string;  
  62. fastcgi_param  REQUEST_METHOD     $request_method;  
  63. fastcgi_param  CONTENT_TYPE       $content_type;  
  64. fastcgi_param  CONTENT_LENGTH     $content_length;  
  65. fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;  
  66. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;  
  67. fastcgi_param  REQUEST_URI        $request_uri;  
  68. fastcgi_param  DOCUMENT_URI       $document_uri;  
  69. fastcgi_param  DOCUMENT_ROOT      $document_root;  
  70. fastcgi_param  SERVER_PROTOCOL    $server_protocol;  
  71. fastcgi_param  REMOTE_ADDR        $remote_addr;  
  72. fastcgi_param  REMOTE_PORT        $remote_port;  
  73. fastcgi_param  SERVER_ADDR        $server_addr;  
  74. fastcgi_param  SERVER_PORT        $server_port;  
  75. fastcgi_param  SERVER_NAME        $server_name;  
  76.  
  77. 並在所支持的主頁面格式中添加php格式的主頁,類似如下:  
  78. location / {  
  79.             root   html;  
  80.             index  index.php index.html index.htm;  
  81.         }  
  82.           
  83. 而後重新載入nginx的配置文件:  
  84. # service nginx restart  
  85.  
  86. 在/usr/html新建index.php的測試頁面,測試php是否能正常工作  
  87. #echo "<?php phpinfo(); ?>>/usr/html/index.php 

測試LNMP的效果:

補充:
編譯安裝好的php,如何動態添加模塊,而無須重新編譯php的方法:
#cd /root/php-5.4.4/ext/sockets/
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config --enable-sockets
#make && make install
編輯php.ini文件
#vim /etc/php.ini
添加以下兩行
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20100525/"
extension = "sockets.so"
重啓web然後使用以下命令查看模塊是否編譯上
#/usr/local/php/bin/php -m

 


二、配置基於域名的虛擬主機

  1. #vim /etc/nginx/nginx.conf  
  2. 在httpd模塊裏添加以下內容  
  3.     server {  
  4.         listen       80;    #監控的端口  
  5.         server_name  www.test.com;  #服務器的域名  
  6.         access_log  /var/log/nginx/www.access;  #這個虛擬主機的訪問日誌  
  7.         error_log  /var/log/nginx/www.err;      #這個虛擬主的的錯誤日誌  
  8.         root       /www/web/test.com;           #網頁存放路徑  
  9.         index  index.php index.html index.htm;  #定義首頁  
  10.        location ~ \.php$ {  
  11.             fastcgi_pass   127.0.0.1:9000;        
  12.             fastcgi_index  index.php;  
  13.             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  14.             include        fastcgi_params;  
  15.         }  
  16.     }  
  17.  
  18. 建立日誌和存放網頁數據目錄     
  19. #mkdir -pv /var/log/nginx    
  20. #mkdir -pv /www/web/test.com  
  21. #cd /www/web/test.com  
  22. #vim index.php  
  23. <h1>www.test.com</h1> 
  24. <?php phpinfo(); ?> 
  25.  
  26. 重啓nginx  
  27. #service nginx restart 

測試:

 


三、開啓監控nginx服務運行狀態,並訪問時基於用戶的認證

注意:開啓監控nginx服務運行狀態需要在編譯nginx時加上這個參數:--with-http_stub_status_module
事先必須有htpasswd命令,這個apache自帶命令,所以需要你事先安裝好httpd

  1. #vim /etc/nginx/nginx.conf  
  2. 添加以下內容  
  3.  server {  
  4.         listen       80;  
  5.         server_name  www.test.com;  
  6.         access_log  /var/log/nginx/www.access;  
  7.         error_log  /var/log/nginx/www.err;  
  8.         root       /www/web/test.com;  
  9.         index  index.php index.html index.htm;  
  10.        location ~ \.php$ {  
  11.             fastcgi_pass   127.0.0.1:9000;  
  12.             fastcgi_index  index.php;  
  13.             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  14.             include        fastcgi_params;  
  15.         }  
  16.         location /nginx_status {        #訪問的時候跟上這個路徑,名字可以隨便取的  
  17.                 stub_status on;         #開啓nginx狀態監控功能    
  18.                 access_log off;         #訪問日誌,這裏是關閉的  
  19.                 auth_basic "Nginx";     #用戶認證跳出框框的名字  
  20.                 auth_basic_user_file "/etc/nginx/.htpasswd"; #指定存放用於認證的賬號和密碼的文件  
  21.         }  
  22.  
  23.     }  
  24.  
  25. #htpasswd -c -m /etc/nginx/.htpasswd nginx  創建.htpasswd隱藏文件和賬號  
  26.  
  27. 重啓nginx  
  28. #service nginx restart 

測試:

點擊確定,查看服務狀態

介紹一下status狀態的參數:
Active connections: 1  #當前 Nginx 正處理的活動連接數

server accepts handled requests   #nginx總共處理了6個連接, 成功創建6次握手 (證明中間沒有失敗的), 總共處理了19個請求

reading  #Nginx讀取到客戶端的Header信息數

writing  #Nginx返回給客戶端的Header信息數

waiting  #開啓 keep-alive 的情況下,這個值等於 active - (reading + writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連接

 


四、啓用基於ssl的認證功能

  1. CA服務器上操作  
  2. #vim /etc/pki/tls/openssl.cnf  
  3. 修改以下內容  
  4. #dir=/etc/pki/CA     
  5. #cd /etc/pki/CA  
  6. #mkdir certs newcerts crl  
  7. #touch index.txt  
  8. #echo 01 > serial  
  9. #(umask 077;openssl genrsa -out private/cakey.pem 2048)生成私鑰  
  10. #openssl req -x509 -new -key private/cakey.pem -out cacert.pem -days 365 生成證書cacert.pem證書  
  11. #mkdir /etc/nginx/ssl  
  12. #cd /etc/nginx/ssl  
  13. #(umask 077;openssl genrsa -out nginx.key 2048)  
  14. #req -new -key nginx.key -out nginx.csr -days 365  
  15. #openssl ca -in nginx.csr -out nginx.crt   
  16.  
  17. 編輯nginx配置文件  
  18. #vim /etc/nginx/nginx.conf  
  19. 添加以下內容  
  20.  server {  
  21.         listen       443;   #SSL的端口爲443  
  22.         server_name  www.test.com;  #要訪問的域名  
  23.           
  24.         ssl                  on;    #啓用ssl功能  
  25.         ssl_certificate      /etc/nginx/ssl/nginx.crt;  #nginx的證書  
  26.         ssl_certificate_key  /etc/nginx/ssl/nginx.key;  #nginx的私鑰  
  27.  
  28.         ssl_session_timeout  5m;        #ssl的會話超時時間  
  29.       
  30.         ssl_protocols  SSLv2 SSLv3 TLSv1; #支持的ssl的各種版本  
  31.         ssl_ciphers  HIGH:!aNULL:!MD5;    #支持的加密算法    
  32.         ssl_prefer_server_ciphers   on;   #建立安全連接,服務器所允許的密碼格式列表   
  33.  
  34.         location / {  
  35.             root   /web/nginx;          #網頁存放路徑  
  36.             index  index.html index.htm;  
  37.         }  
  38.     }  
  39.  
  40. #mkdir -pv /web/nginx #創建目錄  
  41. #echo "<h1>Nginx SSL</h1>>/web/nginx/index.html    #提供一個測試頁面  
  42. #service nginx restart      #重啓nginx                              
  43. #netstat -tlnp |grep 443    #查看443端口 

測試:

 

 


五、安裝Discuz論壇

  1. 這裏已經下載好了源程序,網頁基於上面做的虛擬主機  
  2. 需要在mysql中創建一個數據庫  
  3. #service mysqld start   
  4. #mysqladmin -uroot password 'redhat'    
  5. #mysql -uroot -p  redhat  
  6. #mysql> GRANT ALL ON discuz.* TO root@'%.%.%.%' IDENTIFIED BY 'redhat';    
  7. #mysql> FLUSH PRIVILEGES   
  8. #unzip Discuz_7.2_FULL_SC_GBK.zip  
  9. #cd upload/  
  10. #mv * ../ 

在瀏覽器中輸入:http://www.test.com/install

選擇“我同意”繼續下一步

繼續下一步

 

安裝成功

注意:

1、在安裝論壇的時候,如果提示目錄權限讀寫問題,把nginx的user改成nobody,網頁數據也改成nobody,即可

2、提示數據庫連接不上,提示socket字樣,到/etc/my.cnf裏改一下socket=/tmp/mysqld.sock即可

想要更多瞭解nginx請到官網:http://nginx.org/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章