Nginx源代码安装
Linux下安裝軟件有三種方式,這里我以源代碼編譯安裝為主。服務器最小化安裝后,安裝依賴包。?
?
出于管理和安全的目的,我們希望使用一個指定的普通用戶身份去運行我們的Web服務器。所以,我們首先增加一個普通用戶用于運行我們的Nginx。?
| 1 2 | [root@master?~]#?groupadd?nginx? [root@master?~]#?useradd?-g?nginx?nginx |
關閉系統防火墻,
| 1 2 | [root@master?~]#?service?iptables?stop? [root@master?~]#?chkconfig?iptables?off |
?
1. 下載最新穩定版本并安裝Nginx
然后下載、解壓并編譯安裝我們的Nginx, 這里使用的是最新穩定版本,
| 1 2 3 4 5 6 | [root@master?~]#?wget?http://nginx.org/download/nginx-1.8.0.tar.gz? [root@master?~]#?tar?-xf?nginx-1.8.0.tar.gz?-C?/usr/local/src? [root@master?~]#?cd?/usr/local/src/nginx-1.8.0? [root@master?nginx-1.8.0]#?./configure?--user=nginx?--group=nginx? --with-http_ssl_module?\ --with-http_sub_module |
?
安裝過程比較簡單,./configure過程會報出一些依賴關系,這里一一解決之。首先,操作系統是最小化安裝,并沒有安裝gcc,所以,第一步進行./configure的時候,就會報錯。
當出現如下錯誤時,需要安裝ssl的開發包,
| 1 2 3 4 | ./configure:?error:?SSL?modules?require?the?OpenSSL?library.? You?can?either?do?not?enable?the?modules,?or?install?the?OpenSSL?library? into?the?system,?or?build?the?OpenSSL?library?statically?from?the?source? with?nginx?by?using?--with-openssl=<path>?option. |
當出現如下錯誤時,需要安裝zlib的開發包,
| 1 2 3 4 | ./configure:?error:?SSL?modules?require?the?OpenSSL?library.? You?can?either?do?not?enable?the?modules,?or?install?the?OpenSSL?library? into?the?system,?or?build?the?OpenSSL?library?statically?from?the?source? with?nginx?by?using?--with-openssl=<path>?option. |
| 1 2 3 4 | [root@master?~]#?yum?install?-y?pcre-devel? [root@master?~]#?yum?install?-y?gcc? [root@master?~]#?yum?install?-y?zlib-devel? [root@master?~]#?yum?install?-y?openssl-devel |
下面來看看./configure后面幾個常用的參數:?
| 1 2 3 4 | --prefix=<dir>?????????指定安裝主目錄,默認為/usr/local/nginx? --user=<user>??????????指定用戶身份,如果沒有指定則默認使用nobody? --group=<group>????????指定組身份? --with-http_ssl_module?啟用https支持 |
2. Nginx的啟動、重啟與停止
安裝完畢,我們就可以啟動Nginx了,
| 1 | [root@master?~]#?/usr/local/nginx/sbin/nginx?-c?/usr/loca/nginx/conf/nginx.conf |
-c是用來指定Nginx的主配置文件,如果沒有指定則默認為/usr/loca/nginx/conf/nginx.conf文件。啟動后,可以用ps與netstat命令查看是否啟動成功,
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@master?~]#?ps?-ef?|grep?nginx root??????1059?????1??0?02:49??????????00:00:00?nginx:?master?process?/usr/local/nginx/sbin/nginx?-c?/usr/local/nginx/conf/nginx.conf nginx?????1061??1059??0?02:49??????????00:00:00?nginx:?worker?process?????????????????????????????????????????? root??????1063??1013??0?02:49?pts/0????00:00:00?grep?nginx [root@master?~]#?netstat?-antup Active?Internet?connections?(servers?and?established) Proto?Recv-Q?Send-Q?Local?Address???????????????Foreign?Address?????????????State???????PID/Program?name??? tcp????????0??????0?192.168.1.151:8080??????????0.0.0.0:*???????????????????LISTEN??????1059/nginx?????????? tcp????????0??????0?192.168.1.150:8080??????????0.0.0.0:*???????????????????LISTEN??????1059/nginx?????????? tcp????????0??????0?0.0.0.0:80??????????????????0.0.0.0:*???????????????????LISTEN??????1059/nginx?????????? tcp????????0??????0?0.0.0.0:22??????????????????0.0.0.0:*???????????????????LISTEN??????801/sshd???????????? tcp????????0??????0?127.0.0.1:25????????????????0.0.0.0:*???????????????????LISTEN??????877/master?????????? tcp????????0??????0?192.168.1.129:22????????????192.168.1.106:56004?????????ESTABLISHED?1009/sshd??????????? tcp????????0??????0?:::22???????????????????????:::*????????????????????????LISTEN??????801/sshd???????????? tcp????????0??????0?::1:25??????????????????????:::*????????????????????????LISTEN??????877/master?????????? udp????????0??????0?0.0.0.0:68??????????????????0.0.0.0:*???????????????????????????????1007/dhclient |
啟動成功,我們可以訪問首頁去驗證一下,
3. Nginx啟動腳本
Nginx并沒有提供類似System V服務的管理腳本,如果我們希望開機時要讓Nginx自動啟動,可以執行如下命令:
| 1 2 | [root@master?~]#?echo?“/usr/local/nginx/sbin/nginx?-c?/usr/local/nginx/conf/nginx.conf ”?>>?/etc/rc.local |
當然,如果我們對System V的服務管理腳本情有獨鐘的話,可以參考如下腳本
| 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | [root@master?~]#?cat?/etc/init.d/nginx? #!/bin/sh # #?nginx?-?this?script?starts?and?stops?the?nginx?daemon # #?chkconfig:?-?85?15 #?description:?Nginx?is?an?HTTP(S)?server,?HTTP(S)?reverse?\ #?proxy?and?IMAP/POP3?proxy?server #?processname:?nginx #?config:?/etc/nginx/nginx.conf #?config:?/etc/sysconfig/nginx #?pidfile:?/var/run/nginx.pid #?Source?function?library. .?/etc/rc.d/init.d/functions #?Source?networking?configuration. .?/etc/sysconfig/network #?Check?that?networking?is?up. [?"$NETWORKING"?=?"no"?]?&&?exit?0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename?$nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [?-f?/etc/sysconfig/nginx?]?&&?.?/etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start()?{ ????[?-x?$nginx?]?||?exit?5 ????[?-f?$NGINX_CONF_FILE?]?||?exit?6 ????echo?-n?$"Starting?$prog:?" ????daemon?$nginx?-c?$NGINX_CONF_FILE ????retval=$? ????echo ????[?$retval?-eq?0?]?&&?touch?$lockfile ????return?$retval } stop()?{ ????echo?-n?$"Stopping?$prog:?" ????killproc?$prog?-QUIT ????retval=$? ????echo ????[?$retval?-eq?0?]?&&?rm?-f?$lockfile ????return?$retval ????killall?-9?nginx } restart()?{ ????configtest?||?return?$? ????stop ????sleep?1 ????start } reload()?{ ????configtest?||?return?$? ????echo?-n?$"Reloading?$prog:?" ????killproc?$nginx?-HUP ????RETVAL=$? ????echo } force_reload()?{ ????restart } configtest()?{ ????$nginx?-t?-c?$NGINX_CONF_FILE } rh_status()?{ ????status?$prog } rh_status_q()?{ ????rh_status?>/dev/null?2>&1 } case?"$1"?in ????start) ????????rh_status_q?&&?exit?0 ????????$1 ????????;; ????stop) ????????rh_status_q?||?exit?0 ????????$1 ????????;; ????restart|configtest) ????????$1 ????????;; ????reload) ????????rh_status_q?||?exit?7 ????????$1 ????????;; ????force-reload) ????????force_reload ????????;; ????status) ????????rh_status ????????;; ????condrestart|try-restart) ????????rh_status_q?||?exit?0 ????????;; ????*) ????????echo?$"Usage:?$0?{start|stop|status|restart|condrestart|try-restart|reload|force-reload| configtest}" ????????exit?2 esac |
最后,給腳本一個可執行的權限,然后使用chkconfig命令對其進行管理,
| 1 2 | [root@master?~]#?chmod?755?/etc/init.d/nginx [root@master?~]#?chkconfig?nginx?on |
當我們對Nginx的配置文件做過一些更改后,希望在不中斷當前服務的情況下,進行一個平滑的重啟,可以使用如下命令,
| 1 | [root@master?~]#?service?nginx?reload |
腳本中的reload函數會首先對配置文件做一個語法格式的檢查,使用的是如下命令,
| 1 | [root@master?~]#?/usr/local/nginx/sbin/nginx?-t?-c?/usr/local/nginx/conf/nginx.conf |
當語法格式檢查通過后,會對Nginx發出一個標記為1或者說是HUP的信號,Nginx收到后會關閉舊進程,打開新進程,如果有進程正在為一個用戶提供服務,則會等待這次服務結束。
當然,我們也可以使用service nginx restart的方式去重啟服務。停止Nginx,直接service nginx stop即可,或者kill掉所有的Nginx進程。
本文轉自 ? ?bigstone2012 ? 51CTO博客,原文鏈接:http://blog.51cto.com/lavenliu/1663792
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的Nginx源代码安装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 轻量级DAO层实践初体验
- 下一篇: 在指定位置上方出现通用jquery悬浮提