Linux环境下centos7.6编译安装Nginx
前情提要:centos7.6(64位)
一.安装Nginx
1.下载依赖包并解压安装:
cd /usr/local/src
wget -c https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar zxf openssl-1.1.1g.tar.gz
mv openssl-1.1.1g openssl
wget http://zlib.net/zlib-1.2.11.tar.gz
tar zxf zlib-1.2.11.tar.gz
mv zlib-1.2.11 zlib
wget -c https://sourceforge.net/projects/pcre/files/pcre/8.43/pcre-8.43.tar.gz
tar zxf pcre-8.43.tar.gz
mv pcre-8.43 pcre
2.下载 nginx 并解压:
wget -c http://nginx.org/download/nginx-1.17.5.tar.gz
tar zxf nginx-1.17.5.tar.gz
cd nginx-1.17.5
3.编译配置:
./configure --prefix=/usr/local/nginx --http-client-body-temp-path=/tmp/clientbody --http-proxy-temp-path=/tmp/proxy --http-fastcgi-temp-path=/tmp/fastcgi --http-uwsgi-temp-path=/tmp/uwsgi --http-scgi-temp-path=/tmp/scgi --user=www --group=www --with-file-aio --with-http_realip_module --with-http_ssl_module --with-openssl=/usr/local/src/openssl --with-http_gzip_static_module --with-zlib=/usr/local/src/zlib --with-http_stub_status_module --with-pcre=/usr/local/src/pcre
4.编译安装:
make //若报错可能要: yum install -y gcc-c++
make install
5.防火墙开放 80 端口:
yum install firewalld systemd -y //下载防火墙
systemctl start firewalld.service //开启防火墙
systemctl enable firewalld.service //设置开机启动
firewall-cmd --zone=public --add-port=80/tcp --permanent //开放80端口
6.准备工作 :
mkdir /usr/local/nginx/conf/vhosts
mkdir -p /data/nginx/logs/shop
mkdir /var/run/nginx
# 先cat /etc/passwd | grep www或者cat /etc/group | grep www,若没有www:www
groupadd www
useradd www -g www
chown -R www:www /data/nginx
chown -R www:www /var/run/nginx
mkdir /wwwroot
7.服务脚本文件(/etc/init.d/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
# ccccc
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network 7
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
lockfile="/data/nginx/logs/nginx.lock"
pidfile="/var/run/nginx/nginx.pid"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
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
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
8.添加到系统服务并开机启动 :
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
systemctl restart nginx(开启nginx)
9.查看是否安装成功:
nginx -v
# 若提示nginx不是命令,则vim /etc/profile,在末尾添加:
PATH=$PATH:/usr/local/nginx/sbin
export PATH
再source /etc/profile使配置生效即可
至此,Linux环境下centos7.6编译安装Nginx完毕!