当前位置 主页 > 服务器问题 > Linux/apache问题 >

    centos8自定义目录安装nginx(教程详解)

    栏目:Linux/apache问题 时间:2019-11-29 08:57

    1.安装工具和库

    # PCRE是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式

    # zlib库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip

    yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

    2.目录结构

    源码目录:/home/werben/pkgsrc/nginx
    安装目录:/home/werben/application/nginx

    3.下载解压源码

    # 官网地址: https://nginx.org/en/download.html
    wget -c https://nginx.org/download/nginx-1.17.5.tar.gz

    4.创建用户组和用户

    groupadd www
    useradd -g www www

    5.编译源码

    ./configure --user=www --group=www --prefix=/home/werben/application/nginx --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-pcre
    
    make && make install

    6.映射全局命令

    ln -s /home/werben/application/nginx/sbin/nginx /usr/local/bin/nginx

    7.启动,停止,重启

    nginx -s stop
    nginx -s quit
    ngins -s reload

    8.检测配置文件nginx.conf正确性

    nginx  -t

    9.开机自启动

    vim /lib/systemd/system/nginx.service
    [Unit]
    Description=nginx
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=nginx
    ExecReload=nginx reload
    ExecStop=nginx quit
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    #重新加载守护进程
    systemctl daemon-reload
    
    #启动nginx服务
    systemctl start nginx.service
    #停止nginx服务
    systemctl stop nginx.service
    #设置开机自启动
    systemctl enable nginx.service
    #停止开机自启动
    systemctl disable nginx.service
    #查看服务当前状态
    systemctl status nginx.service
    #重新启动服务
    systemctl restart nginx.service
    #查看所有已启动的服务
    systemctl list-units --type=service

    10.出现问题和解决方法

    #如果`systemctl start nginx.service`提示如下报错
    
    Job for nginx.service failed because the control process exited with error code.
    See "systemctl status nginx.service" and "journalctl -xe" for details.
    
    #执行
    systemctl status nginx.service
    #如果出现如下错误
      Process: 35783 ExecStart=...nginx/sbin/nginx(code=exitedstatus=203/EXEC)
      nginx.service: Control process exited, code=exited status=203
      systemd[1]: nginx.service: Failed with result 'exit-code'.
      localhost.localdomain systemd[1]: Failed to start nginx.
    
    
    journalctl -xe
    
    #如果看到如下信息     
      If you believe that systemd should be allowed execute access on the>
      Then you should report this as a bug.
      You can generate a local policy module to allow this access.
      Do allow this access for now by executing:
      # ausearch -c '(nginx)' --raw | audit2allow -M my-nginx
      # semodule -X 300 -i my-nginx.pp
    
    
    #解决方法
    setenforce 0
    vim /etc/selinux/config
    SELINUX=disabled