nginx服务安装 附一键安装脚本

一、手动安装

yLinux(CentOS7.X)安装Nginx

二、一键安装脚本

#!/bin/bash

#关闭firewalld防火墙
systemctl stop firewalld
systemctl disable firewalld
systemctl mask firewalld

#关闭selinux
setenforce 0   #临时关闭
sed -i  '/^SELINUX/ s/enforcing/disabled/' /etc/selinux/config #永久关闭

#新建用户,用来启动nginx
useradd  -s /sbin/nologin nginx
#解决依赖关系,以及安装常用工具
yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ automake autoconf make
yum install -y psmisc lsof net-tools vim wget

#下载nginx压缩包
mkdir  -p  /nginx
cd /nginx
curl  -O http://nginx.org/download/nginx-1.20.2.tar.gz

#解压文件
tar xf  nginx-1.20.2.tar.gz 
cd nginx-1.20.2

#编译前的配置工作,根据实际需要选择常用模块
./configure --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/logs/nginx.pid --user=nginx --group=nginx --build=scweb_server --with-threads  --with-file-aio --with-http_v2_module --with-http_ssl_module --with-stream --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

#允许2个编译命令同时执行,可根据内核线程数适量增加(linux环境下nproc命令可查看线程数)
make -j 2

#编译安装
make install

#添加到系统服务,设置开机自启
echo '[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target' > /usr/lib/systemd/system/nginx.service

chmod 755 /usr/lib/systemd/system/nginx.service

#延迟读取pid文件,避免提前读取pid文件(nginx启动需要时间)报错
mkdir -p /etc/systemd/system/nginx.service.d
printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf

#添加软链接和设置环境变量选其一
#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
#将nginx配置到系统环境变量中
echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/profile

#nginx配置文件高亮显示
mkdir -p  ~/.vim/syntax
cd ~/.vim/syntax
wget -O nginx.vim  http://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

#日志轮转
echo '/usr/local/nginx/logs/*.log {
    create 0640 nginx root
    daily
    rotate 10
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}
' > /etc/logrotate.d/nginx

systemctl daemon-reload
systemctl enable nginx  #开机自启
systemctl status nginx

三、问题篇

1,问题:
有可能80端口被占用,或者有死进程

 2,解决方法:
查看端口或者查看进程
停掉80端口服务,或者杀死进程
在启动nginx服务

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

無薪法师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值