CentOS6.5 下Nginx 的安裝與配置


 

 

昨天買了個服務器最近在配置一些基礎環境,想在訪問www.wzpbk.com:8080  不想要后面的:8080就能直接訪問到,聽說了Nginx就研究下給服務器裝上傳說中大名鼎鼎 Nginx 他能反向代理服務器及郵件服務器,具有占用內存少,並發能力強的優點,已被廣泛應用。

 

1.安裝必須環境

 

nginx的編譯需要c++,同時prce(重定向支持)和openssl(https支持)也需要安裝。

請順序安裝依賴

1  yum install gcc-c++  
2  yum -y install pcre*  
3  yum -y install openssl* 

2.,下載nginx-1.9.9.tar.gz,可放在 /usr/local/ 目錄下   ps:我這里使用的老本你可以嘗試新版

1  [root@admin ~]# cd /usr/local/  
2  [root@admin local]# wget http://nginx.org/download/nginx-1.9.9.tar.gz 

3.解壓及編譯

1 [root@admin local]# tar -zxvf nginx-1.9.9.tar.gz 

4.進入nginx目錄

1 [root@admin local]# cd nginx-1.9.9 

5.設置安裝目錄為 /usr/local/nginx

1 [root@admin nginx-1.9.9]# ./configure --prefix=/usr/local/nginx 

開始編譯安裝

1  [root@admin nginx-1.9.9]# make  
2  [root@admin nginx-1.9.9]# make install  

我這里是阿里服務器在(控制台)實例中配置一下端口,,本機中再開放一下80端口(不知道請查我的博客中有講過如何開放指定端口);

 

 

 

啟動nginx服務

進入安裝目錄 /usr/local/nginx

1 [root@admin ~]# cd /usr/local/nginx  
2 [root@admin sbin]# ./nginx

查看進程,可以看到nginx的master和worker進程

1     [root@admin sbin]# ps -ef | grep nginx  
2     root     32150     1  0 13:28 ?        00:00:00 nginx: master process ./nginx  
3     nobody   32151 32150  0 13:28 ?        00:00:00 nginx: worker process  
4     root     32154 28494  0 13:28 pts/1    00:00:00 grep nginx  

可以通過訪問ip:80測試,看到頁面這樣的提示就說明安裝成功

重啟的命令:

1 [root@admin sbin]# ./nginx -s reload 

最后可以配置一下命令 添加nginx為系統服務(service nginx start/stop/restart)

1、在/etc/init.d/目錄下編寫腳本,新建名為nginx的文件然后把腳本代碼粘貼進去 注意:配置 文件位置我的在usr/local/下若果不是這個路徑你需要修改)

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

然后執行

2 [root@example ~]# chmod 755 /etc/init.d/nginx
3 [root@example ~]# chkconfig --add nginx

nginx啟動、停止、無間斷服務重啟

1 [root@example ~]# service nginx start
2 
3 [root@example ~]# service nginx stop
4 
5 [root@example ~]# service nginx reload

好了蕾絲(@.@)

注補充:

  停止服務  查進程號之后 kill -9 進程號 即可.

關於怎么配置nginx等我研究下;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM