1.在/usr/lib/systemd/system目录下面配置nginx.service内容
echo '[Unit]
Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
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
[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service
示例中编译安装nginx的位置是/usr/local/nginx目录,根据实际情况修改
授权
chmod 755 /usr/lib/systemd/system/nginx.service
2.开启服务并设置开机自启
systemctl daemon-reload
systemctl enable --now nginx
systemctl status nginx
3.nginx卸载方式
先停nginx(生产环境先迁移其他服务),再卸载
yum安装
yum remove -y nginx
两条命令选其一
#find / -name nginx -exec rm -rf {} \;
find / -name nginx | xargs rm -rf
编译安装直接删除相关文件即可
find / -name nginx | xargs rm -rf
4.附nginx一键安装脚本
#!/bin/bash
#新建用户,用来启动nginx
useradd -s /sbin/nologin nginx
#解决依赖关系,以及安装常用工具
yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ automake autoconf make
#下载nginx压缩包
mkdir -p /nginx
cd /nginx
curl -O http://nginx.org/download/nginx-1.24.0.tar.gz
#解压文件
tar xf nginx-1.24.0.tar.gz
cd nginx-1.24.0
#编译前的配置工作,根据实际需要选择常用模块
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--http-log-path=/usr/local/nginx/logs/access.log --error-log-path=/usr/local/nginx/logs/error.log \
--with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module \
--with-stream --with-stream_realip_module --with-http_gzip_static_module --with-file-aio --with-threads
#允许多个编译命令同时执行,可根据CPU核心数适量增加(linux环境下nproc命令可查看)
make -j 12
#编译安装
make install
#添加到系统服务,设置开机自启
echo '[Unit]
Description=The nginx web and reverse proxy server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
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
[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service
chmod 755 /usr/lib/systemd/system/nginx.service
#将nginx配置到系统环境变量中
echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/profile
#nginx配置文件高亮显示
mkdir -p ~/.vim/syntax
cd ~/.vim/syntax
wget -O nginx.vim https://www.vim.org/scripts/download_script.php?src_id=19394
echo "au BufRead,BufNewFile /usr/local/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif " >> ~/.vim/filetype.vim
systemctl daemon-reload
systemctl enable --now nginx
systemctl status nginx