Linux環境安裝 - Nginx

概述

Nginx是一款輕量級的網頁服務器、反向代理服務器。相較於Apache、lighttpd具有佔有內存少,穩定性高等優勢。它最常的用途是提供反向代理服務

安裝

1.安裝依賴

yum install gcc
yum install pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
//一鍵安裝上面4個依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下載Nginx的tar包

創建一個文件夾
cd /usr/local/
mkdir nginx
cd nginx
// 下載tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.g

3.安裝Nginx

//進入nginx目錄
cd /usr/local/nginx
//執行命令
./configure --prefix=/usr/local/nginx \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--user=nginx \
--group=nginx \
--error-log-path=/logs/nginx/errpr.log \
--http-log-path=/logs/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi

參數解釋

–prefix=/usr/local/nginx ==>安裝部署後的根目錄,默認爲/usr/local/nginx
–conf-path=/etc/nginx/nginx/nginx.conf ==>配置文件的放置路徑,默認/conf/nginx.conf
–user=nginx ==>指定worker進程運行時所屬的用戶
–group=nginx ==>指定worker進程運行是所屬的組
–error-log-path=/var/log/nginx/errpr.log ==>error日誌放置位置
–http-log-path=/var/log/nginx/access.log ==>access日誌放置的位置
–pid-path=/var/run/nginx/nginx.pid ==>pid文件的存放路徑;默認/logs/nginx.pid
–lock-path=/var/lock/nginx.lock ==>lock文件的放置路徑;默認/logs/nginx.lock
–with-http_ssl_module ==>提供HTTPS服務;該模塊的安裝依賴於OpenSSL開源軟件
–with-http_stub_status_module ==>能夠獲取Nginx自上次啓動以來的工作狀態
–with-http_gzip_static_module ==> 如果採用gzip 模塊把一些文檔進行gzip 格式壓縮後再返回給客戶端,那麼對同一個文件每次都會重新壓縮,這是比較消耗服務器CPU 資源的. gzip static 模塊可以在做gzip 壓縮前,先查看相同位置是否有已經做過gzip 壓縮的.gz 文件,如果有,就直接返回。這樣就可以預先在服務器上做好文檔的壓縮,給CPU 減負
–wiht-http_flv_modle ==>可以在向客戶端返回響應肘,對FLV 格式的視頻文件在header 頭做一些處理,使得客戶端可以觀看、拖動FLV 視頻
–with-http_mp4_module ==>使客戶端可以觀看、拖動MP4 視頻
–http-client-body-temp-path=/var/tmp/nginx/client ==>set path to store http client request body temporary files
–http-proxy-temp-path=/var/tmp/nginx/proxy ==>Nginx 作爲HTTP 反向代理服務器時,上游服務器產生的HTTP 包體在需要臨時存放到磁盤文件時,這樣的臨時文件將放到該路徑下;默認/proxy _temp
–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi ==>Fastcgi 所使用臨時文件的放置目;默認/fastcgi_temp

// 執行make命令
make
// 執行make install命令
make install

4.啓動Nginx

//測試配置文件
/nginx/sbin/nginx -t
//啓動命令
/nginx/sbin/nginx
//停止命令
/nginx/sbin/nginx -s stop
//重啓命令
/nginx/sbin/nginx -s reload

5.Nginx設置開機啓動

//創建nginx執行文件
vim /etc/init.d/nginx

nginx腳本添加如下命令

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/logs/nginx/lock/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

腳本地址 http://wiki.nginx.org/RedHatNginxInitScript
記得修改文件中命令爲自己的路徑

//啓動/停止腳本
/etc/init.d/nginx start
/etc/init.d/nginx stop
// 將nginx服務加入chkconfig管理列表中
chkconfig --add /etc/init.d/nginx
// service對nginx進行操作 
service nginx start
service nginx stop
//設置終端模式開機啓動
chkconfig nginx on

安裝問題

1.如何查看Nginx啓動使用的配置文件

/nginx/sbin/nginx -t

這裏寫圖片描述

2.錯誤提示 make[1]:leaving directory ‘/usr/local/nginx

如果存在該目錄,可以不用管

3.訪問php頁面,變成直接下載

查找Nginx使用的配置文件 /nginx/sbin/nginx -t (因爲安裝後在/usr/local/nginx/conf下也有配置文件nginx.conf),然後檢查nginx.conf的配置是否已經開啓對.php的解析,配置如下:

4.設置了開機啓動nginx服務後,service nginx status 顯示啓動失敗
解決方法:
需要先把/sbin/nginx啓動的進程kill掉,然後再使用service/systemctl 命令進行啓動,如果還是出現問題,建議重啓阿里雲服務器在進行操作

5.nginx: [emerg] getpwnam(“nginx”) failed

沒有安裝nginx用戶導致無法啓動

useradd -s /sbin/nologin -M nginx

參考

https://www.cnblogs.com/jimisun/p/8057156.html
https://blog.csdn.net/u013870094/article/details/52463026

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章