centos7 安装 nginx

本文详细描述了如何在Linux系统上通过源码方式安装Nginx1.18.0,包括gcc、perl和zlib库的安装,配置步骤,编译安装,以及将其设置为系统服务,包括防火墙管理和开机自启动的设置。
摘要由CSDN通过智能技术生成

一、源码方式安装:

1.安装gcc
yum install -y gcc

2.安装perl库
yum install -y pcre pcre-devel

3.安装zlib库
yum install -y zlib zlib-devel

4.到nginx 官网下载nginx 压缩包 

官网地址:https://nginx.org/

我用的版本:nginx-1.18.0.tar.gz 

5.将下载好的 nginx-1.18.0.tar.gz 包 上传到 linux服务器某位置 如:/usr/local/tools ,

然后解压: tar zxvf  nginx-1.8.0.tar.gz ,进入解压后的nginx-1.18.0 目录:

[root@cnetos7-101 tools]# cd /usr/local/tools/nginx-1.18.0

6.配置安装路径及各参数:

[root@cnetos7-101 nginx-1.18.0]  

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre

执行结果显示:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

7.编译和安装

[root@cnetos7-101 nginx-1.18.0]   make && make install

8.安装完成后 在 /usr/local 中生成nginx 目录;

9.启动nginx:

        cd /usr/local/nginx/sbin

        在nginx下的sbin文件夹下执行  ./nginx

10.nginx 基本操作命令:

./nginx # 启动

./nginx -s stop #快速停止

./nginx -s quit #优雅关闭,在退出前完成已经接受的连接请求

./nginx -s reload #重新加载配置

11.如果显示running,说明防火墙正在运行中,请关闭防火墙或者开放80端口

  关闭防火墙,开机禁止自启动:
  systemctl stop firewalld.service

  systemctl disable firewalld.service

  不关闭防火墙,开放80端口:

  查看防火墙永久开放端口列表,发现为空。
  firewall-cmd --list-ports --permanent

  永久开放80端口,并再次查看开放端口

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

重载防火墙配置,如果不重载配置或重启防火墙。配置不生效,也会访问不到80端口
firewall-cmd --reload

12.安装成系统服务,可以使用 systemctl 命令启停nginx服务

12.1创建服务脚本

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

        录入一下内容:

[Unit]
Description=nginx web server
After=network.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
PrivateTmp=true

[Install]
WantedBy=multi-user.target

13.重载系统配置

        systemctl daemon-reload

14.重启服务,并设置开机自启动
        systemctl start nginx.service

        systemctl enable nginx.service

15.systemctl操作nginx 常用命令:

1)启动nginx

systemctl start nginx

2)停止服务

systemctl stop  nginx

3)重启服务

systemctl restart nginx

4)重新加载

systemctl reload nginx 

 配置文件位置:/usr/local/nginx/conf

 配置文件内容:

#user  nobody;
worker_processes  1;

error_log  logs/error.log;

pid  logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    limit_req_zone $binary_remote_addr zone=ConnLimitZone:10m rate=10r/s;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile on;
    tcp_nopush on;
    keepalive_timeout  65;
    #include vhost/*.conf;  #新增

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        
        location /xxx-admin {
                root /usr/local/project/crmeb/admin/dist;                                                 
                try_files $uri $uri/ /index.html;
                
        }
        
    
        location /xxx-admin/api/ {
            
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Scheme  $scheme;
            client_body_buffer_size 10m;
            proxy_connect_timeout 7200;
            proxy_send_timeout 7200;
            proxy_read_timeout 7200;
            proxy_buffer_size 32k;
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            
            proxy_pass http://192.168.124.101:8080/;
            
            expires 5s;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值