centos7.2编译Nginx-1.10.2
一、编译环境安装
安装gcc编译环境,安装下载工具wget。
yum -y groupinstall "Development Tools"
yum -y install wget
二、下载安装文件并解压
从官方网站下载Nginx,以及依赖的pcre,openssl和zlib,需要注意版本,不要使用新版的pcre2。
#切换目录
cd /usr/local/src
#下载依赖文件pcre,openssl,zlib
wget -c http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.bz2
wget -c https://www.openssl.org/source/openssl-1.0.2j.tar.gz
wget -c http://zlib.net/zlib-1.2.8.tar.gz
#下载Nginx
wget -c http://Nginx.org/download/Nginx-1.10.2.tar.gz
解压文件。
tar -zxvf Nginx-1.10.2.tar.gz
tar -jxvf pcre-8.38.tar.bz2
tar -zxvf zlib-1.2.8.tar.gz
tar -zxvf openssl-1.0.2j.tar.gz
三、用户和目录准备
#新建系统账号Nginx
useradd -r Nginx -s /sbin/nologin -M
#新建Nginx需要的目录
cd /var/tmp/
mkdir -p /var/tmp/Nginx/{client_body,proxy,fastcgi,uwsgi,scgi}
chown -R Nginx /var/tmp/Nginx
四、编译Nginx-1.10.2
具体编译参数,需要依据实际情况修改。
cd /usr/local/src/Nginx-1.10.2
./configure \
--prefix=/usr/local/Nginx \ --sbin-path=/usr/sbin/Nginx \ --conf-path=/etc/Nginx/Nginx.conf \ --error-log-path=/var/log/Nginx/error.log \ --http-log-path=/var/log/Nginx/access.log \ --pid-path=/var/run/Nginx.pid \ --lock-path=/var/lock/Nginx.lock \ --user=Nginx \ --group=Nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre=../pcre-8.38 \ --with-zlib=../zlib-1.2.8 \ --with-openssl=../openssl-1.0.2j \ --with-debug \ --http-client-body-temp-path=/var/tmp/Nginx/client_body \ --http-proxy-temp-path=/var/tmp/Nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/Nginx/fastcgi \ --http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/Nginx/scgi \ --with-stream make
make install
五、配置Nginx启动脚本
vi /etc/init.d/Nginx
chmod u+x /etc/init.d/Nginx
chkconfig --add Nginx
chkconfig Nginx on
Nginx脚本内容如下,可根据实际情况修改Nginx和Nginx_CONF_FILE参数。
#! /bin/bash
#
# 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
# 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/Nginx.lock
start() {
[ -x $Nginx ] || exit 5
[ -f $Nginx_CONF_FILE ] || exit 6
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
六、启动Nginx
#启动Nginx服务
systemctl start Nginx.service
#查看端口监听
ss -tlnp|grep :80
参考文章:
1.https://typecodes.com/web/centos7compilenginx.html
2.https://typecodes.com/web/nginxserviceoptshell.html
3.http://nginx.org/en/docs/configure.html