1. 程式人生 > >Centos7安裝ngnix方法和步驟

Centos7安裝ngnix方法和步驟

  • 新增資源庫

              在 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 install nginx

安裝成功:
測試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


說明配置檔案成功!
centos7.0+ nginx實現停止、啟動、重啟
在CentOS7中,進行chkconfig命令操作時會發現有類似“systemctl.....”的提示,systemctl可以簡單實現service和chkconfig的結合,這樣通過一個命令就可以實現兩個命令的功能。
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

修改/etc/nginx/nginx.conf
   #user nginx;
   user  root;
worker_processes  1;
修改/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
       # root   /usr/share/nginx/html;
        root   /wwwroot/images;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
       # root   /usr/share/nginx/html;
         root   /wwwroot/images;
    }