手把手带你在Linux centos7环境下安装Nginx 包括https开机自启等

1.安装gcc,执行命令进行安装:

yum -y install gcc automake autoconf libtool make

2.安装pcre-devel :

yum install -y pcre pcre-devel

3.安装zlib :

yum install -y zlib zlib-devel

4.安装OpenSSL :

yum install -y openssl openssl-devel

5.进入Nginx压缩包所在目录:

cd /usr/local/nginx

6.解压:

tar -zxvf nginx-1.18.0.tar.gz

7.解压完成后,进入Nginx软件所在目录:

cd nginx-1.18.0

8.配置 支持HTTPS

./configure --prefix=/usr/local/nginx --with-http_ssl_module

10.编译:

make

11.安装:

make install

12.查看Nginx是否安装成功:

cd /usr/local/nginx/sbin/

./nginx -t

显示如下图,说明安装成功
在这里插入图片描述

13.关闭防火墙

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

不关闭也可以 防火墙自行打开nginx的端口

14.设置开机启动
① 在系统/etc/init.d/目录下创建nginx文件:

vi /etc/init.d/nginx

② 将以下内容添加到该文件中:

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx server"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

③ 保存退出后,设置文件的执行权限:

chmod a+x /etc/init.d/nginx

④ 将服务加入chkconfig管理列表:

chkconfig --add /etc/init.d/nginx

⑤ 设置终端模式开机启动:

chkconfig nginx on

⑥ 启动和停止、重载配置命令:

service nginx start #启动
service nginx stop #停止
service nginx reload #重载配置文件

测试是否启动成功:在浏览器输入:http://IP,出现下图界面,说明安装启动成功

curl http://127.0.0.1:80/

手把手带你在Linux centos7环境下安装Nginx 包括https开机自启等_第1张图片

你可能感兴趣的:(Linux)