1. 安装依赖包
//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2. 下载并解压安装包
// 创建文件夹softwar
cd /usr/local
mkdir softwar
// 下载tar包
wget http://nginx.org/download/nginx-1.25.0.tar.gz
// 解压tar包
tar -vxf nginx-1.25.0.tar.gz
// 进入目录 nginx-1.25.0 执行命令 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
// 执行命令
make && make install
3. 启动nginx服务
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4. 配置ngin.conf
# 打开配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.12.132;# 这里指定当前服务器的IP地址
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/src/html;# 静态编译文件位置
index index.html index.htm;
}
5. 重启nginx
/usr/local/nginx/sbin/nginx -s reload
6. 设置开机自启动
cd /etc/systemd/system
# 创建开机自启动脚本
vim nginx.service
# 以下是自启动脚本内容
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
7. nginx启动命令
# 设置开机自启动
systemctl enable nginx
# 启动nginx命令
systemctl start nginx.service
# 重启nginx
systemctl restart nginx.service
# 查看当前nginx服务状态
systemctl status nginx.service
# 停止开机自启动
systemctl disable nginx.service