CentOS 7 安裝 Nginx (LNMP環境搭建第一步)

Linux 的安裝方式有兩種,能夠是yum安裝,也能夠是源碼安裝。html

  • yum安裝:簡單方便,不易出錯。
  • 源碼安裝:繁瑣,易出錯,但服務性能好。

yum安裝

1.安裝nginx

1.1yum 安裝nginx很是簡單,就輸入一條命令便可。nginx

sudo yum -y install nginx   # 安裝 nginx
sudo yum remove nginx  # 卸載 nginx

1.2安裝完後,先不要急着操做,此時的nginx服務尚未啓動,c++

查看nginx狀態(未啓動前)
    命令:systemctl status nginx.service

1.3啓動nginx服務sql

  命令:sudo service nginx startvim

1.4查看nginx的狀態及進程與端口(啓動後)
    命令:systemctl status nginx.service

1.5設置開始啓動安全

  命令:sudo systemctl enable nginx服務器

使用yum進行Nginx安裝時,Nginx配置文件在/etc/nginx目錄下。性能

2.nginx 相關服務配置命令 

sudo systemctl enable nginx # 設置開機啓動 
sudo service nginx start # 啓動nginx服務
sudo service nginx stop # 中止nginx服務
sudo service nginx restart # 重啓nginx服務
sudo service nginx reload # 從新加載配置,通常是在修改過nginx配置文件時使用。
systemctl status nginx.service #查看nginx的狀態及進程與端口
netstat -antp | grep :80 #查看80端口被哪一個服務佔用
netstat -antp | grep : #查看全部端口占用狀況
ps aux | grep nginx #查看nginx進程運行狀態
nginx -V #查看nginx版本

 

源碼包安裝(推薦)

Nginx源碼包安裝方式步驟比較繁瑣,而且須要提早安裝一些Nginx依賴庫。ui

1、依賴庫安裝

1. 安裝 gcc 環境(阿里雲新買的服務器通常都預安裝了gcc,此步能夠跳過)this

sudo yum -y install gcc gcc-c++ # nginx編譯時依賴gcc環境

2. 安裝 pcre

sudo yum -y install pcre pcre-devel # 讓nginx支持重寫功能

3.安裝 zlib

# zlib庫提供了不少壓縮和解壓縮的方式,nginx使用zlib對http包內容進行gzip壓縮
sudo yum -y install zlib zlib-devel 

4. 安裝 openssl

# 安全套接字層密碼庫,用於通訊加密
sudo yum -y install openssl openssl-devel

以上安裝完成後,進行nginx安裝。

2、nginx 源碼包安裝

將準備好的 nginx-1.17.8.tar.gz包,拷貝至/usr/local/nginx目錄下(通常習慣在此目錄下進行安裝,新買服務器沒有此目錄,要自行建立)進行解壓縮。
源碼包下載地址:https://nginx.org/en/download.html ,請根據本身的需求選擇相應的版本進行安裝

1. 建立/usr/local/nginx目錄,進入/usr/local/nginx目錄wget安裝包(不想用ftp上傳,直接在服務器wget獲取安裝包)

#建立 /usr/local/nginx 目錄
mkdir /usr/local/nginx

#獲取nginx包
wget https://nginx.org/download/nginx-1.17.8.tar.gz

2. 解壓

tar -zxvf nginx-1.17.8.tar.gz

3. 在完成解壓縮後,進入nginx-1.17.8目錄進行源碼編譯安裝。

cd nginx-1.17.8

./configure --prefix=/usr/local/nginx # 檢查平臺安裝環境
#--prefix=/usr/local/nginx  是nginx編譯安裝的目錄(推薦),安裝完後會在此目錄下生成相關文件

若是前面的依賴庫都安裝成功後,執行./configure --prefix=/usr/local/nginx命令會顯示一些環境信息。若是出現錯誤,通常是依賴庫沒有安裝完成,可按照錯誤提示信息進行所缺的依賴庫安裝。

4. 進行源碼編譯並安裝nginx

make # 編譯
make install # 安裝

5. 源碼包安裝與yum安裝的nginx服務操做命令也不一樣。

  • 啓動服務
/usr/local/nginx/sbin/nginx
  • 從新加載服務
/usr/local/nginx/sbin/nginx -s reload
  • 中止服務
/usr/local/nginx/sbin/nginx -s stop
  • 查看nginx服務進程
ps -ef | grep nginx # 查看服務進程

6. 訪問

 3、配置nginx開機自啓

1.先找出nginx配置文件路徑和nginx路徑(後面會用到)

查找nginx配置文件路徑 find / -name nginx.conf(兩個路徑均可以,本人使用第一個)

 在nginx安裝目錄/user/local/nginx查找nginx路徑  find /usr/local/nginx/ -name nginx(使用包含sbin目錄路徑)

2.編輯 vim /etc/init.d/nginx  添加一下內容  【使用Notepad++編輯並轉換成Unix格式再上傳,否則文件沒法生效,本人在此踩過坑,Notepad++依次點擊 編輯/文檔格式轉換/轉爲 Unix(LF)

根據路徑修改nginx安裝地址、nginx配置文件路徑兩處,上面第一步已查出路徑。

  1 #!/bin/sh
  2 # nginx - this script starts and stops the nginx daemon
  3 #
  4 # chkconfig:   - 85 15
  5 # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
  6 #               proxy and IMAP/POP3 proxy server
  7 # processname: nginx
  8 # config:      /usr/local/nginx/nginx-1.17.8/conf/nginx.conf
  9 # config:      /etc/sysconfig/nginx
 10 # pidfile:     /usr/local/tools/nginx/nginx-1.14.2/logs/nginx.pid
 11 
 12 # Source function library.
 13 . /etc/rc.d/init.d/functions
 14 
 15 # Source networking configuration.
 16 . /etc/sysconfig/network
 17 
 18 # Check that networking is up.
 19 [ "$NETWORKING" = "no" ] && exit 0
 20 
 21 #nginx安裝地址(根據本身安裝路徑填寫)
 22 nginx="/usr/local/nginx/sbin/nginx"
 23 prog=$(basename $nginx)
 24 
 25 #nginx配置文件路徑(根據本身安裝路徑填寫)
 26 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 27 
 28 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 29 
 30 lockfile=/usr/local/tools/nginx/nginx-1.14.2/logs/lock/subsys/nginx
 31 
 32 make_dirs() {
 33    # make required directories
 34    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
 35    if [ -n "$user" ]; then
 36       if [ -z "`grep $user /etc/passwd`" ]; then
 37          useradd -M -s /bin/nologin $user
 38       fi
 39       options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 40       for opt in $options; do
 41           if [ `echo $opt | grep '.*-temp-path'` ]; then
 42               value=`echo $opt | cut -d "=" -f 2`
 43               if [ ! -d "$value" ]; then
 44                   # echo "creating" $value
 45                   mkdir -p $value && chown -R $user $value
 46               fi
 47           fi
 48        done
 49     fi
 50 }
 51 
 52 start() {
 53     [ -x $nginx ] || exit 5
 54     [ -f $NGINX_CONF_FILE ] || exit 6
 55     make_dirs
 56     echo -n $"Starting $prog: "
 57     daemon $nginx -c $NGINX_CONF_FILE
 58     retval=$?
 59     echo
 60     [ $retval -eq 0 ] && touch $lockfile
 61     return $retval
 62 }
 63 
 64 stop() {
 65     echo -n $"Stopping $prog: "
 66     killproc $prog -QUIT
 67     retval=$?
 68     echo
 69     [ $retval -eq 0 ] && rm -f $lockfile
 70     return $retval
 71 }
 72 
 73 restart() {
 74     configtest || return $?
 75     stop
 76     sleep 1
 77     start
 78 }
 79 
 80 reload() {
 81     configtest || return $?
 82     echo -n $"Reloading $prog: "
 83     killproc $nginx -HUP
 84     RETVAL=$?
 85     echo
 86 }
 87 
 88 force_reload() {
 89     restart
 90 }
 91 
 92 configtest() {
 93   $nginx -t -c $NGINX_CONF_FILE
 94 }
 95 
 96 rh_status() {
 97     status $prog
 98 }
 99 
100 rh_status_q() {
101     rh_status >/dev/null 2>&1
102 }
103 
104 case "$1" in
105     start)
106         rh_status_q && exit 0
107         $1
108         ;;
109     stop)
110         rh_status_q || exit 0
111         $1
112         ;;
113     restart|configtest)
114         $1
115         ;;
116     reload)
117         rh_status_q || exit 7
118         $1
119         ;;
120     force-reload)
121         force_reload
122         ;;
123     status)
124         rh_status
125         ;;
126     condrestart|try-restart)
127         rh_status_q || exit 0
128             ;;
129     *)
130         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
131         exit 2
132 esac

3. 添加可執行權限  chmod +x /etc/init.d/nginx

4.開啓自啓  chkconfig nginx on

重啓服務:reboot,訪問頁面正常,nginx已自動啓動

 

nginx安裝:https://blog.csdn.net/sqlquan/article/details/101099850

nginx開機自啓:http://www.javashuo.com/article/p-ercfvfzx-bm.html

相關文章
相關標籤/搜索