目录
1、环境准备
1.1 关闭防火墙(为了测试)
systemctl status firewalld.service(查看防火墙状态)
systemctl stop firewalld.service (关闭防火墙)
systemctl start firewalld.service (开启防火墙)
systemctl disable firewalld.service (禁止防火墙自启动)
systemctl enable firewalld.service (防火墙随系统开启启动)
1.2 安装所需插件
# gcc 可以编译 C,C++,Ada,Object C和Java等语言(安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境)
yum install -y gcc-c++
# pcre pcre-devel pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel
# openssl openssl-devel OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用
yum install -y zlib zlib-devel
# nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库
yum install -y openssl openssl-devel
1.3 下载nginx
#nginx官网地址
https://nginx.org/en/download.html
2、安装nginx
#下载
wgte https://nginx.org/download/nginx-1.21.6.tar.gz
#解压并进入
tar xvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
#配置(带有https模块)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
#编译和安装
编译:make
安装:make install
#查看安装路径
whereis nginx
3、设置systemd服务管理nginx,并设置开机自启动
#检查mysql是否启动
ps -ef |grep nginx
ss -nlt |grep 80
#如启动则需先关闭
······
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#启动nginx服务
systemctl start nginx.service
#停止服务
systemctl stop nginx.service
#重新启动服务
systemctl restart nginx.service
#查看所有已启动的服务
systemctl list-units --type=service
#查看服务当前状态
systemctl status nginx.service
#设置开机自启动
systemctl enable nginx.service
# 停止开机自启动
systemctl disable nginx.service
4、添加环境变量
cat >> /etc/profile << EOF
export PATH=$PATH:/usr/local/nginx/sbin
EOF
source /etc/profile