Nginx在CentOS7中的编译安装成系统服务

1.初始Nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了 IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的 Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004 年10月4日。 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配 置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3) 代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实 上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站 用户有:百度、京东、新浪、网易、腾讯、淘宝等。

2.Nginx在CentOS7中的编译安装成系统服务

  • 下载

    官方下载地址:http://nginx.org/

    选中最新版 nginx-1.21.6 ,进入下载页面,点击1.21.6版本下载

  • 编译安装与依赖检查

    推荐使用root用户安装

    上传nginx-1.21.6.tar.gz上传到linux文件系统,解压tar包

    tar -zxvf nginx-1.21.6.tar.gz

    进入解压了的目录下

    执行 ./configure --prefix=/usr/local/nginx 进行安装,可能会缺少某些依赖

    --prefix参数为安装位置

    如果提示或警告

    checking for OS
    + Linux 3.10.0-693.el7.x86_64 x86_64
    checking for C compiler ... not found
    ./configure: error: C compiler cc is not found

    解决办法:安装gcc

    yum install -y gcc

    提示

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.

    解决办法:安装perl库

    yum install -y pcre pcre-devel

    提示

    ./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib=<path> option.

    安装zlib库

    yum install -y zlib zlib-devel

    再次执行上面安装命令,如看到一下信息为安装成功

    接下来依次执行

    make
    make install

    若无报错信息则安装完成

  • Nginx启动与停止

    进入 /usr/local/nginx 安装目录,文件目录结构如下

    sbin/nginx 启动
    sbin/nginx -s stop 快速停止
    sbin/nginx -s quit 优雅的关闭,在退出前完成已经接受的连接请求
    sbin/nginx -s reload 重新加载配置

    手动启动nginx服务

    sbin/nginx

    查看服务是否启动,在浏览器输入虚拟机的ip,若无法访问,而nginx启动没有报错,那就是防火墙的问题

  • 关于防火墙

    关闭防火墙

    systemctl stop firewalld.service

    禁止防火墙开机启动

    systemctl disable firewalld.service

    放行端口

    firewall-cmd --zone=public --add-port=80/tcp --permanent

    重启防火墙

    firewall-cmd --reload

    关闭防火墙后,在启动Nginx,访问浏览器,出现一下界面启动成功

        

 

  • 安装成系统服务

    由于手动停止关闭过于繁琐,所以安装成系统服务

    创建服务脚本

    vi /usr/lib/systemd/system/nginx.service

    服务脚本的内容

    [Unit]
    Description=nginx - web server
    After=network.target remote-fs.target nss-lookup.target
    ​
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    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 stop
    ExecQuit=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true
    ​
    [Install]
    WantedBy=multi-user.target

    注意:如果使用xshell终端工具复制粘贴到vi编辑器时,要切换到插入模式再粘贴,否则内容会缺失。还有,脚本内容里面的路径为安装Nginx的路径,本机的安装路径为【/usr/local/nginx】,如果和我的路径不一样,需要修改一下!

    重新加载系统服务

    systemctl daemon-reload

    注意:先别着急启动,先把刚才启动的Nginx服务停止

    查看nginx进程是否存在,【grep -v grep】是不查找使用grep命令查找nginx的进程

    ps -ef | grep nginx | grep -v grep

    停止Nginx服务

    sbin/nginx -s stop

    开启Nginx服务

    systemctl start nginx

    查看Nginx服务状态

    systemctl status nginx

    出现【active (running)】表示服务正在运行

    设置开机启动

    systemctl enable nginx.service

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值