Nginx 是 C語言 開發,建議在 Linux 上運行,固然,也能夠安裝 Windows 版本,本篇則使用 CentOS 7 做爲安裝環境。html
一. gcc 安裝
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:linux
yum install gcc-c++
二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:nginx
yum install -y pcre pcre-devel
三. zlib 安裝
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。c++
yum install -y zlib zlib-devel
四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。正則表達式
yum install -y openssl openssl-devel
1.直接下載.tar.gz
安裝包,地址:https://nginx.org/en/download.html算法
2.使用wget
命令下載(推薦)。shell
wget -c https://nginx.org/download/nginx-1.13.7.tar.gz
依然是直接命令:安全
tar -zxvf nginx-1.13.7.tar.gz cd nginx-1.13.7
其實在 nginx-1.10.1 版本中你就不須要去配置相關東西,默認就能夠了。固然,若是你要本身配置目錄也是能夠的。
1.使用默認配置bash
./configure
make make install
查找安裝路徑:測試
whereis nginx
cd /usr/local/nginx/sbin/ ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload
./nginx -s quit
:此方式中止步驟是待nginx進程處理任務完畢進行中止。
./nginx -s stop
:此方式至關於先查出nginx進程id再使用kill命令強制殺掉進程。
查詢nginx進程:
ps aux|grep nginx
2.從新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效須要重啓 nginx,使用-s reload
不用先中止 ngin x再啓動 nginx 便可將配置信息在 nginx 中生效,以下:
./nginx -s reload
即在rc.local
增長啓動代碼就能夠了。
vi /etc/rc.local
增長一行 /usr/local/nginx/sbin/nginx
設置執行權限:
chmod 755 rc.local
到這裏,nginx就安裝完畢了,啓動、中止、重啓操做也都完成了,
一。首先寫一個shell腳本,腳本名稱:nginx
#! /bin/bash # chkconfig: 35 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse set -e PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME SCRIPTNAME=/etc/init.d/$NAME test -x $DAEMON || exit 0 d_start(){ $DAEMON || echo -n " already running" } d_stop() { $DAEMON -s quit || echo -n " not running" } d_reload() { $DAEMON -s reload || echo -n " counld not reload" } case "$1" in start) echo -n "Starting $DESC:$NAME" d_start echo "." ;; stop) echo -n "Stopping $DESC:$NAME" d_stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." d_reload echo "reloaded." ;; restart) echo -n "Restarting $DESC: $NAME" d_stop sleep 2 d_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2 exit 3 ;; esac exit 0
2、將shell腳本放入到 /etc/rc.d/init.d/中,並執行下列命令
1:chmod +x /etc/rc.d/init.d/nginx (設置可執行權限)
2:chkconfig --add nginx (添加系統服務)
3、系統服務啓動,重啓,中止命令
service nginx start 啓動nginx service nginx restart 重啓nginx service nginx stop 中止nginx service nginx reload 從新加載nginx配置