Linux-Centos7 下編譯安裝nginx (附nginx開機啟動指令碼)
1.安裝編譯所需環境:
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.下載原始碼包(這裡用的是nginx-1.14.0)
wget http://nginx.org/download/nginx-1.14.0.tar.gz
3.編譯安裝
---新增使用者和組
groupadd www
useradd -g www www
---解壓壓縮包,並cd進去
#tar -xvzf nginx-1.14.0.tar.gz #cd nginx-1.14.0/ #./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module #make && make install
---nginx模板粗略解釋
nginx大部分常用模組,編譯時./configure --help以--without開頭的都預設安裝。
--prefix=PATH : 指定nginx的安裝目錄。預設 /usr/local/nginx
--conf-path=PATH : 設定nginx.conf配置檔案的路徑。nginx允許使用不同的配置檔案啟動,通過命令列中的-c選項。預設為prefix/conf/nginx.conf
--user=name: 設定nginx工作程序的使用者。安裝完成後,可以隨時在nginx.conf配置檔案更改user指令。預設的使用者名稱是nobody。--group=name類似
--with-pcre : 設定PCRE庫的原始碼路徑,如果已通過yum方式安裝,使用--with-pcre自動找到庫檔案。使用--with-pcre=PATH時,需要從PCRE網站下載pcre庫的原始碼(版本4.4 - 8.30)並解壓,剩下的就交給Nginx的./configure和make來完成。perl正則表示式使用在location指令和 ngx_http_rewrite_module模組中。
--with-zlib=PATH : 指定 zlib(版本1.1.3 - 1.2.5)的原始碼解壓目錄。在預設就啟用的網路傳輸壓縮模組ngx_http_gzip_module時需要使用zlib 。
--with-http_ssl_module : 使用https協議模組。預設情況下,該模組沒有被構建。前提是openssl與openssl-devel已安裝
--with-http_stub_status_module : 用來監控 Nginx 的當前狀態
--with-http_realip_module : 通過這個模組允許我們改變客戶端請求頭中客戶端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意義在於能夠使得後臺伺服器記錄原始客戶端的IP地址
--add-module=PATH : 新增第三方外部模組,如nginx-sticky-module-ng或快取模組。每次新增新的模組都要重新編譯(Tengine可以在新加入module時無需重新編譯)
4.驗證是否安裝成功,並建立軟連線
#/usr/local/nginx/sbin/nginx -V //下方為輸出顯示結果 nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module #ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
5.啟動檢視web介面
#/usr/bin/nginx //啟動nginx
#lsof -i:80 //檢視埠
#curl localhost:80 //是否啟動,下方為顯示結果
[[email protected] nginx-1.14.0]# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
6.設定開機啟動指令碼
一:建立指令碼
# vim /etc/init.d/nginx
#!/bin/sh
# tiger
# 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: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/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=/var/lock/subsys/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
二:賦予指令碼可執行許可權
# chmod a+x /etc/init.d/nginx
三:將nginx服務加入chkconfig管理列表
# chkconfig --add /etc/init.d/nginx
# chkconfig nginx on