服務器快速集成Nginx

服務器快速集成Nginx

1.安裝依賴

1.1.pcre重定向依賴

yum -y install pcre*
  • PCRE(Perl Compatible Regular Expressions)是一個Perl庫,不止具有http重定向依賴,還包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。

1.2.openssl(http/https支持,如果不需要https可以不安裝。

yum -y install pcre*
  • OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。

1.3.安裝 Tengine 執行配置命令 gcc環境

yum -y install gcc gcc-c++ autoconf automake make  
  • 若是本機上已經安裝則可以忽略,主要用於編譯Nginx源碼

2.下載解壓

2.1.下載

wget http://nginx.org/download/nginx-1.12.2.tar.gz

2.2.解壓

先創建Nginx存放路徑,再進行解壓

cd /usr/local/
mkdir nginxgz
tar -zxvf nginx-1.7.8.tar.gz -C /usr/local/nginxgz

3.編譯安裝Nginx

cd /usr/local/nginxgz/nginx-1.12.2
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre
  • –with-http_ssl_module : 支持https
  • –with-http_stub_status_module : 支持nginx狀態查詢
  • –with-http_v2_module : 支持Google發明的SPDY協議,必須有ssl的支持
  • –with-pcre : 支持rewrite重寫功能,必須有pcre
    注:若是沒有安裝gcc則會報如下錯誤:
checking for OS
 + Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found

編譯:

make
make install

4.操作Nginx

  • 啓動:
/usr/local/nginx/sbin/nginx
  • 重啓:
/usr/local/nginx/sbin/nginx -s reload
  • 關閉:
/usr/local/nginx/sbin/nginx -s stop

5.配置防火牆(iptables)測試

5.1.關閉防火牆

service iptables stop

5.2.編輯配置文件

vi /etc/sysconfig/iptables

5.3.開發80端口

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

5.4.重啓服務

service iptables restart

測試,瀏覽器輸入服務器地址(如:192.168.1.111)

6.將Nginx添加到服務(service)中

  • 6.1.切換到啓動腳本
cd /etc/init.d
  • 6.2.創建nginx文件
touch nginx
  • 6.3.編輯nginx文件
vim /etc/init.d/nginx
  • 6.4.並且添加以下內容
#!/bin/bash  
# nginx Startup script for the Nginx HTTP Server  
#  
# 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=/usr/local/nginx/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: " 
 $nginxd -s reload  
    #if your nginx version is below 0.8, please use this command: "kill -HUP `cat ${nginx_pid}`" 
    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 

注:

# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
  • 腳本中這兩個不能去除,不然不能啓動,而且會報以下錯誤,即語法錯誤
nginx 服務不支持 chkconfig
  • chkconfig語法:
語 法:chkconfig [--add][--del][--list][系統服務] 或 chkconfig [--level <等級代號>][系統服務][on/off/reset]
  • 6.5.保存退出後,切換路徑
cd /etc/rc.d/init.d
  • 6.6.設置權限
chmod +x nginx
  • 6.7.chkconfig改變nginx運行級別
 /sbin/chkconfig --level 345 nginx on

到此爲止:
任何位置都能運行 service nginx start 可選 start | stop | restart | reload | status | help

發佈了51 篇原創文章 · 獲贊 30 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章