前言
最近新申請了一台服務器,需要安裝下nginx服務,安裝nginx不是第一次,之前反反復復也裝過,由於原先筆記都寫在網易有道雲中,趁着這次機會重新排版截圖完善下。
版本
系統:CentOS-6.4 (系統有點老,手頭就這一個鏡像)
nginx:nginx-1.18.0
關注本文末尾微信公眾號,回復“666”獲取常用開發工具包,內含常用開發組件,節省FQ下載時間。
安裝
1.下載
打開nginx官網
http://nginx.org/en/download.html
一般選擇穩定版本,復制下載鏈接
http://nginx.org/download/nginx-1.18.0.tar.gz
下載路徑為 /opt/nginx/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2.安裝依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
執行命令后等待安裝完即可
3.編譯安裝
解壓安裝文件
tar -zxvf nginx-1.18.0.tar.gz
進入解壓后目錄執行編譯
./configure --with-http_ssl_module
編譯參數有很多,這邊我只增加了SSL模塊,小伙伴可以根據自身情況調整
--prefix=PATH:指定 nginx 的安裝目錄
--conf-path=PATH:指定 nginx.conf 配置文件路徑
--user=NAME:nginx 工作進程的用戶
--with-pcre:開啟 PCRE 正則表達式的支持
--with-http_ssl_module:啟動 SSL 的支持
--with-http_stub_status_module:用於監控 Nginx 的狀態
--with-http-realip_module:允許改變客戶端請求頭中客戶端 IP 地址
--with-file-aio:啟用 File AIO
--add-module=PATH:添加第三方外部模塊
執行完上述命令后,在解壓目錄下,多出一個Makefile文件
執行make命令
make
執行make install 命令
make install
因編譯時未指定安裝目錄,執行make install 命令后看到反饋日志信息,實際安裝目錄為/usr/local/nginx
期間都沒報任何錯誤,nice。
4.啟動
進入實際安裝目錄,看看,並在其sbin目錄下執行啟動nginx
cd /usr/local/nginx/
服務已啟動后,訪問一下吧,OK
常用命令執行也正常,到這里nginx的安裝已完成了,但有一點每次需要執行nginx命令時都需要指定執行文件后再進行相應命令執行,有點繁瑣,怎樣能更便捷、優雅點呢?往下看
5.服務啟動
nginx官網已經給我們提供了一個腳本,官網也寫的很清楚,將腳本信息保存以nginx命名的文件中並至放至系統/etc/init.d目錄下即可
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
腳本命令如下,有2個配置項需要根據實際情況進行相應調整。
nginx執行文件路徑,已調整為本例中路徑
nginx="/usr/local/nginx/sbin/nginx"
nginx配置文件,已調整為本例中路徑
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
以下是官方的腳本信息
#!/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
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
fi
}
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 $prog -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
根據自身實際情況調整腳本后,復制至指定目錄下即可
cp nginx /etc/init.d/
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
執行完上述命令后即可使用服務命令操作nginx服務啦,趕緊試一把吧。
注意事項:
(增加於 2020-11-26)
當更改為上述2個配置項后,還有個地方需特別注意,如未調整當執行命令時則有可能出現下面錯誤
[root@iZbp1dmodzug1ak3zqlzrnZ sbin]# service nginx start
Starting nginx (via systemctl): Job for nginx.service failed because a configured resource limit was exceeded. See "systemctl status nginx.service" and "journalctl -xe" for details.
執行 systemctl status nginx.service 命令后,發現問題所在
[root@iZbp1dmodzug1ak3zqlzrnZ sbin]# systemctl status nginx.service
● nginx.service - SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: failed (Result: resources) since Thu 2020-11-26 13:19:08 CST; 5min ago
Docs: man:systemd-sysv-generator(8)
Process: 6617 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
Nov 26 13:19:08 iZbp1dmodzug1ak3zqlzrnZ systemd[1]: Starting SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server...
Nov 26 13:19:08 iZbp1dmodzug1ak3zqlzrnZ systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
Nov 26 13:19:08 iZbp1dmodzug1ak3zqlzrnZ systemd[1]: Failed to start SYSV: NGINX is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
Nov 26 13:19:08 iZbp1dmodzug1ak3zqlzrnZ systemd[1]: Unit nginx.service entered failed state.
Nov 26 13:19:08 iZbp1dmodzug1ak3zqlzrnZ systemd[1]: nginx.service failed.
Nov 26 13:20:15 iZbp1dmodzug1ak3zqlzrnZ systemd[1]: Unit nginx.service cannot be reloaded because it is inactive.
既然pid文件無法讀取到,那我這邊就將配置文件中pid的路徑調整至該目錄下即可。
更改完后,重新執行啟動命令,沒有報任何錯誤,啟動正常。如果啟動還是報錯,可以將nginx服務先kill掉,再用下面服務命令啟動即可。
service nginx start
service nginx start #啟動
service nginx stop #停止
service nginx status #服務狀態
service nginx restart #重啟
參考資料
http://nginx.org/en/download.html
https://www.jb51.net/article/72527.htm