Nginx實戰基礎篇一 源碼包編譯安裝部署web服務器

Nginx實戰基礎篇一源碼包編譯安裝部署web服務器

 

實驗步驟:
一、安裝nginx必須的依賴包 

 

  1. [root@rhel6u3-7 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel  //yum創建過程略,安裝略 
二、安裝編譯nginx,目前系統測試環境爲rhel6.3  軟件版本爲nginx-1.27
 

 

  1. [root@rhel6u3-7 ~]# useradd nginx -s /sbin/nologin //給nginx服務器創建後臺進程管理用戶 
  2. [root@rhel6u3-7 ~]# tar zxvf nginx-1.2.7.tar.gz  //解壓縮 
  3. [root@rhel6u3-7 ~]# cd nginx-1.2.7  
  4. [root@rhel6u3-7 nginx-1.2.7]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module   
  5. // --user=nginx –group=nginx 設置允許nginx允許的用戶和組爲nginx 
  6. // --prefix=/usr/local/nginx/  設置nginx安裝路徑 
  7. // --with-http_stub_status_module 安裝允許狀態模塊 
  8. // --with-http_ssl_module 安裝ssl模塊 
  9. //更多參數請參看 ./configure --help 
  10. ……  //安裝顯示略,如果配置正確,最後會顯示以下信息。 
  11. Configuration summary 
  12.   + using system PCRE library 
  13.   + using system OpenSSL library 
  14.   + md5: using OpenSSL library 
  15.   + sha1: using OpenSSL library 
  16.   + using system zlib library 
  17.   nginx path prefix: "/usr/local/nginx/" 
  18.   nginx binary file: "/usr/local/nginx//sbin/nginx" 
  19.   nginx configuration prefix: "/usr/local/nginx//conf" 
  20.   nginx configuration file: "/usr/local/nginx//conf/nginx.conf" 
  21.   nginx pid file: "/usr/local/nginx//logs/nginx.pid" 
  22.   nginx error log file: "/usr/local/nginx//logs/error.log" 
  23.   nginx http access log file: "/usr/local/nginx//logs/access.log" 
  24.   nginx http client request body temporary files: "client_body_temp" 
  25.   nginx http proxy temporary files: "proxy_temp" 
  26.   nginx http fastcgi temporary files: "fastcgi_temp" 
  27.   nginx http uwsgi temporary files: "uwsgi_temp" 
  28.   nginx http scgi temporary files: "scgi_temp" 
  29. [root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx  –V  //安裝完成後查看Nginx的相關環境配置信息是否正確 
  30. nginx version: nginx/1.2.7 
  31. built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC)  
  32. TLS SNI support enabled 
  33. configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module 
  34. [root@rhel6u3-7 ~]# 
  35. [root@rhel6u3-7 nginx-1.2.7]# make & make install   //編譯安裝過程略 

三、通過nginx自身腳本機器nginx服務器,並通過各種命令查看是否啓動成功
 

 

  1. [root@rhel6u3-7 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf   // -c指向nginx主配置文件 
  2. [root@rhel6u3-7 ~]# lsof -i :80  //查看nginx進程號及運行情況 
  3. COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 
  4. nginx   4080  root    6u  IPv4  21467      0t0  TCP *:http (LISTEN) 
  5. nginx   4081 nginx    6u  IPv4  21467      0t0  TCP *:http (LISTEN) 
  6. [root@rhel6u3-7 ~]# ps -ef | grep nginx  //查看nginx進程號及運行情況 
  7. root      4080     1  0 22:24 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
  8. nginx     4081  4080  0 22:24 ?        00:00:00 nginx: worker process                                           
  9. root      4088  1433  0 22:27 pts/0    00:00:00 grep nginx 
  10. [root@rhel6u3-7 ~]# netstat -nltp | grep 80  //查看nginx進程監聽端口 
  11. tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4080/nginx  
 
,測試nginx配置的默認web服務器是否能正常運行
通過linux自帶命令links 測試

 

  1. [root@rhel6u3-7 ~]# links 127.0.0.1  //出現 welcome to nginx!說明nginx服務啓動成功
通過windows服務器IE瀏覽器測試

 
擴展知識:
1、設置nginx開機自動啓動,將nginx啓動命令添加到/etc/rc.local 

 

  1. [root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local  
  2. [root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx 
  3. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 

2、通過設置System V 腳本,使用server命令啓動nginx服務

 

  1. [root@rhel6u3-7 init.d]# vim  nginx  //在/etc/init.d/下創建nginx啓動腳本文件 
  2. #!/bin/sh 
  3. # nginx - this script starts and stops the nginx daemon 
  4. # chkconfig: - 85 15 
  5. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  6. #   proxy and IMAP/POP3 proxy server 
  7. # processname: nginx 
  8. # config: /etc/nginx/nginx.conf 
  9. # config: /etc/sysconfig/nginx 
  10. # pidfile: /var/run/nginx.pid 
  11. # Source function library. 
  12. . /etc/rc.d/init.d/functions 
  13. # Source networking configuration. 
  14. . /etc/sysconfig/network 
  15. # Check that networking is up. 
  16. [ "$NETWORKING" = "no" ] && exit 0 
  17.     nginx="/usr/local/nginx/sbin/nginx" 
  18.     prog=$(basename $nginx) 
  19.     NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 
  20. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  21.     lockfile=/var/lock/subsys/nginx 
  22.  
  23. start() { 
  24.     [ -x $nginx ] || exit 5 
  25.     [ -f $NGINX_CONF_FILE ] || exit 6 
  26.     echo -n $"Starting $prog: " 
  27.     daemon $nginx -c $NGINX_CONF_FILE 
  28.     retval=$? 
  29.     echo 
  30. [ $retval -eq 0 ] && touch $lockfile 
  31.     return $retval 
  32.  
  33. stop() { 
  34.     echo -n $"Stopping $prog: " 
  35.     killproc $prog -QUIT 
  36.     retval=$? 
  37.     echo 
  38. [ $retval -eq 0 ] && rm -f $lockfile 
  39.     return $retval 
  40.     killall -9 nginx 
  41.  
  42. restart() { 
  43.     configtest || return $? 
  44.     stop 
  45.     sleep 1 
  46.     start 
  47.  
  48. reload() { 
  49.     configtest || return $? 
  50.     echo -n $"Reloading $prog: " 
  51.     killproc $nginx -HUP 
  52.     RETVAL=$? 
  53.     echo 
  54.  
  55. force_reload() { 
  56.     restart 
  57.  
  58. configtest() { 
  59.     $nginx -t -c $NGINX_CONF_FILE 
  60.  
  61. rh_status() { 
  62.     status $prog 
  63.  
  64. rh_status_q() { 
  65.     rh_status >/dev/null 2>&1 
  66.  
  67. case "$1" in 
  68.     start) 
  69.         rh_status_q && exit 0 
  70.         $1 
  71.     ;; 
  72.     stop) 
  73.         rh_status_q || exit 0 
  74.         $1 
  75.     ;; 
  76.     restart|configtest) 
  77.         $1 
  78.     ;; 
  79.     reload) 
  80.         rh_status_q || exit 7 
  81.         $1 
  82.     ;; 
  83.     force-reload) 
  84.         force_reload 
  85.     ;; 
  86.     status) 
  87.         rh_status 
  88.     ;; 
  89.     condrestart|try-restart) 
  90.         rh_status_q || exit 0 
  91.     ;; 
  92.     *) 
  93.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  94.         exit 2 
  95. esac 

 

  1. [root@rhel6u3-7 init.d]# chmod 755 nginx   //修改腳本文件nginx的權限 
  2. [root@rhel6u3-7 init.d]# chkconfig --add nginx  //將腳本文件加入chkconfig中 
  3. [root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on  //設置nginx開機在3和5級別自動啓動 
  4. [root@rhel6u3-7 init.d]# chkconfig --list | grep nginx 
  5. nginx           0:off   1:off   2:off   3:on    4:off   5:on    6:off 
 
測試nginx腳本文件是否能夠正常使用
 

 

  1. [root@rhel6u3-7 init.d]# /etc/init.d/nginx restart  //重新啓動 
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
  4. Stopping nginx:                                            [  OK  ] 
  5. Starting nginx:                                            [  OK  ] 
  6. [root@rhel6u3-7 init.d]# /etc/init.d/nginx reload  //不間斷服務平滑重啓 
  7. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
  8. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
  9. Reloading nginx:                                           [  OK  ] 
  10. [root@rhel6u3-7 ~]# cat /usr/local/nginx/logs/nginx.pid  //存儲了nginx運行的進程號 
  11. 15799 
  12. [root@rhel6u3-7 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //不間斷服務重新啓動nginx 
  13.  [root@rhel6u3-7 init.d]# /etc/init.d/nginx stop 
  14. Stopping nginx:                                            [  OK  ] 
  15. [root@rhel6u3-7 init.d]# killall -9 nginx  //結束nginx的全部經常 

 

  1. [root@rhel6u3-7 html]# pwd  //安裝nginx完成後,默認網站的路徑 
  2. /usr/local/nginx/html 
  3. [root@rhel6u3-7 html]# ll    //可以查看到默認網站爲index.html,內容爲以上測試內容。 
  4. total 8 
  5. -rw-r--r-- 1 root root 537 Feb 25 22:21 50x.html 
  6. -rw-r--r-- 1 root root 612 Feb 25 22:21 index.html 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章