CentOS 安裝和配置 Nginx

查看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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容