美文网首页程序员
CentOS 安装和配置 Nginx

CentOS 安装和配置 Nginx

作者: Sunsol | 来源:发表于2017-07-10 11:22 被阅读0次

    查看CentOS的版本

    使用一下命令来查看一下当前系统的版本

    [root@localhost ~]# cat /etc/redhat-release
    CentOS Linux release 7.2.1511 (Core)
    

    添加资源库

    在 CentOS 系统上安装 Nginx ,你得先去添加一个资源库,像这样:

    vim /etc/yum.repos.d/nginx.repo
    

    使用 vim 命令去打开 /etc/yum.repos.d/nginx.repo ,如果 nginx.repo 不存在,就会去创建一个这样的文件,打开以后按一下小 i 键,进入编辑模式,然后复制粘贴下面这几行代码,完成以后按 esc 键退出,再输入:wq (保存并退出)

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1
    

    yum 命令去安装 nginx

    完成上边操作以后,我们就可以使用 yum 命令去安装 nginx 了

    yum install nginx
    

    安装成功:

    Loaded plugins: fastestmirror, langpacks
    nginx                                                    | 2.9 kB     00:00     
    nginx/7/x86_64/primary_db                                  |  14 kB   00:01     
    Loading mirror speeds from cached hostfile
     * base: mirrors.sina.cn
     * extras: mirrors.btte.net
     * updates: mirrors.sina.cn
    Resolving Dependencies
    --> Running transaction check
    ---> Package nginx.x86_64 1:1.10.1-1.el7.ngx will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package       Arch           Version                       Repository     Size
    ================================================================================
    Installing:
     nginx         x86_64         1:1.10.1-1.el7.ngx            nginx         640 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package
    
    Total download size: 640 k
    Installed size: 2.1 M
    Is this ok [y/d/N]: y
    Downloading packages:
    nginx-1.10.1-1.el7.ngx.x86_64.rpm                          | 640 kB   00:27     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : 1:nginx-1.10.1-1.el7.ngx.x86_64                              1/1 
    ----------------------------------------------------------------------
    
    Thanks for using nginx!
    
    Please find the official documentation for nginx here:
    * http://nginx.org/en/docs/
    
    Commercial subscriptions for nginx are available on:
    * http://nginx.com/products/
    
    ----------------------------------------------------------------------
      Verifying  : 1:nginx-1.10.1-1.el7.ngx.x86_64                              1/1 
    
    Installed:
      nginx.x86_64 1:1.10.1-1.el7.ngx                                               
    
    Complete!
    

    测试nginx配置文件

    当你执行 nginx -t 得时候,nginx会去测试你得配置文件得语法,并告诉你配置文件是否写得正确,同时也告诉了你配置文件得路径:

    nginx -t
    

    打印如下:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful 
    

    说明配置文件成功!

    配置域名指向

    查看/etc/nginx/nginx.conf文件,内容如下:

    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        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  /var/log/nginx/access.log  main;
        sendfile        on;
        #tcp_nopush     on;
        keepalive_timeout  65;
        #gzip  on;
        include /etc/nginx/conf.d/*.conf;
    }
    

    最后一行可以看出nginx的配置都在conf.d文件中,而且是读取改文件夹下所有的.conf文件。所以我们可以在这个文件夹下随意的创建配置文件,比如test.conf,其内容如下:

    server {
        listen 80;
        server_name test.hahaha.com;
        client_max_body_size 64M;
        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
    

    首先需要从万网上边把这个域名给指向到这个服务器的IP上来。

    这个时候,用户访问 test.hahaha.com ,万网就会指向到这个服务器上,然后 nginx 默认监听的是80端口,这样,nginx 就会接收到这个请求,随后,nginx 通过以上的配置,把请求指向到本服务器上的 3000 端口的服务商

    停止、启动、重启

    在CentOS7中,进行chkconfig命令操作时会发现有类似systemctl.....的提示,systemct 可以简单实现servicechkconfig的结合,这样通过一个命令就可以实现两个命令的功能。
    systemctl命令的基本操作格式是:

    systemctl [OPTIONS...]  {COMMAND}...
    

    以nginx服务为例,实现停止、启动、重启的动作如下:

    systemctl stop    nginx.service
    systemctl start   nginx.service
    systemctl restart nginx.service
    

    检查服务状态

    systemctl status nginx.service
    

    使服务开机启动

    systemctl enable nginx.service
    

    取消服务开机启动

    systemctl disable nginx.service
    

    相关文章

      网友评论

        本文标题:CentOS 安装和配置 Nginx

        本文链接:https://www.haomeiwen.com/subject/ipyyhxtx.html