Centos7安裝nginx

下載

官網下載地址://nginx.org/en/download.html
Mainline version 開發版
Stable version 穩定版
Legacy versions 歷史版

鼠標移動到你要選擇的版本超鏈接上點右鍵 複製鏈接地址

下載:wget //nginx.org/download/nginx-1.16.0.tar.gz

 

安裝

移走:mv ./nginx-1.16.0.tar.gz /usr/local/

解壓:tar -zxvf nginx-1.16.0.tar.gz

進去:cd nginx-1.16.0

配置:./configure –with-http_stub_status_module –with-http_ssl_module

安裝:make && make install

 

配置

打開配置文件:vim /usr/local/nginx/conf/nginx.conf

一個監聽是一個server{….}段落,裏面默認的server代碼塊可以刪掉。

當你在你的域名控制台(阿里雲或騰訊雲或百度雲等等)中設置域名解析成當前服務器IP時,你就可以在這裡配置域名監聽。

比如下面這段:

    server {
        listen       80;
        server_name  www.abc.com abc.com;
        root /www/abc;
        location / {
           index.html;
        }
     }

當瀏覽器訪問 www.abc.com時,由於域名解析指向當前服務器,所以網絡請求會發往當前服務器的80端口,nginx監聽到請求後,會去找到root指定的地址 也就是/www/abc目錄,將目錄中的index.html返回給請求方,此時瀏覽器收到返回,解析標籤,展示在頁面上。

 

命令

編寫服務腳本:vim /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: /usr/local/nginx/logs/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/logs/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 /usr/local/nginx/logs/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

PS:經常粘貼前兩行貼不全,小心檢查。

 

設置權限:chmod 755 /etc/init.d/nginx

 

自啟配置:vim /etc/rc.local

在文件末尾新增一行:/usr/local/nginx/sbin/nginx

 

服務命令啟動:/etc/init.d/nginx start

開機自啟:chkconfig nginx on

 

可使用的命令:

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl reload nginx
systemctl restart nginx