Nginx安裝環境
nginx是C語言開發,建議在linux上運行,本教程使用Centos7.0作爲安裝環境。
- gcc 安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:
yum install gcc-c++
- pcre PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
- zlib zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y zlib zlib-devel
- openssl OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。 nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
yum install -y openssl openssl-devel
編譯安裝
將nginx-1.8.0.tar.gz拷貝至linux服務器。解壓:
//分配權限,不然的話沒有權限解壓 chmod -R 777 nginx-1.8.0.tar.gz tar -zxvf nginx-1.8.0.tar.gz cd nginx-1.8.0
創建Makefile,把下面的命令複製到命令行中就會自動創建Makefile:
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
注意:上邊將臨時文件目錄指定爲/var/temp/nginx,需要在/var下創建temp及nginx目錄
編譯及安裝,在上邊的--prefix=/usr/local/nginx \
指定了安裝的目錄:
make make install
nginx的可執行文件在nginx–>sbin下的nginx文件。
啓動Nginx
啓動nginx,進入sbin目錄./nginx
[root@niaoyun49938 sbin]# ./nginx
判斷是否啓動成功,在瀏覽器中輸入你的IP地址,nginx默認端口是80,出現下面的界面表示啓動成功。
查看nginx的進程:
[root@niaoyun49938 sbin]# ps -ef|grep nginx root 93542 1 0 14:51 ? 00:00:00 nginx: master process ./nginx nobody 93543 93542 0 14:51 ? 00:00:00 nginx: worker process
93542是nginx的主進程的進程Id,93543是nginx的工作進程的進程Id。
停止Nginx
方式一,快速停止
./nginx -s stop
快速停止,此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
方式二,完整停止
./nginx -s quit
完整停止(建議使用),此方式停止步驟是待nginx進程處理任務完畢進行停止。
重啓Nginx
方式一,先停止再啓動
./nginx -s quit ./nginx
對nginx進行重啓相當於先停止nginx再啓動nginx,即先執行停止命令再執行啓動命令。
方式二,重新加載配置文件
./nginx -s reload
當nginx的配置文件nginx.conf修改後,要想讓配置生效需要重啓nginx,使用-s reload不用先停止nginx再啓動nginx即可將配置信息在nginx中生效。
開機自啓動Nginx
編寫shell腳本
這裏使用的是編寫shell腳本的方式來處理,編輯nginx文件:
vi /etc/init.d/nginx
複製一下代碼:
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
:wq 保存並退出。
設置文件的訪問權限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用戶可執行)
這樣在控制檯就很容易的操作nginx了:查看Nginx當前狀態、啓動Nginx、停止Nginx、重啓Nginx…
[root@niaoyun49938 init.d]# ./nginx status ./nginx: line 1: nx: command not found ./nginx: line 19: [: =: unary operator expected nginx is stopped [root@niaoyun49938 init.d]# ./nginx stop ./nginx: line 1: nx: command not found ./nginx: line 19: [: =: unary operator expected Stopping nginx: [FAILED] [root@niaoyun49938 init.d]# ./nginx start ./nginx: line 1: nx: command not found ./nginx: line 19: [: =: unary operator expected Starting nginx: [ OK ] [root@niaoyun49938 init.d]# ./nginx restart ./nginx: line 1: nx: command not found ./nginx: line 19: [: =: unary operator expected Stopping nginx: [ OK ] Starting nginx: [ OK ] [root@niaoyun49938 init.d]#
如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加載新的配置文件並運行,可以將此命令加入到rc.local文件中,這樣開機的時候nginx就默認啓動了
加入到rc.local文件中
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存並退出,下次重啓會生效。