*以下都是在centos7系統下進行
一.安裝
- 添加yum源
sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安裝
sudo yum install nginx
- 配置服務
- 設置開機啟動
sudo systemctl enable nginx
- 啟動服務
sudo systemctl start nginx
- 重啟服務
sudo systemctl restart nginx
- 停止服務
sudo systemctl stop nginx
- 重新加載,因為一般重新配置之后,不希望重啟服務,這時可以使用重新加載。
sudo systemctl reload nginx
- 設置開機啟動
二.config配置
自動安裝的nginx配置文件一般都在 /etc/nginx下面。
我們需要編輯 /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 /root/monkey-help/UIServer/dist; # vue的文件所在目錄,主要為dist
index index.html index.htm; # dist下面的index
}
以下可以不用管
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
三.問題總結
- 服務器策略組需要配置端口允許出入
- 服務器內部防火牆需要打開端口
- 如遇到403,有四種情況,解決方法如下:
- 1、查看主進程與功能進程是否為同一用戶啟動。
ps aux | grep "nginx: worker process" | awk '{print $1}'
如果不是同一用戶需要修 /etc/nginx/nginx.conf 文件。修改如下:user root; # 主要修改這個 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; }
- 2、缺少index.html或者index.php文件,就是配置文件中index index.html index.htm這行中的指定的文件
- 3、權限問題,如果nginx沒有web目錄的操作權限,也會出現403錯誤。解決辦法:修改web目錄的讀寫權限,或者是把nginx的啟動用戶改成目錄的所屬用戶,重啟Nginx即可解決
sudo chmod -R 777 目錄path
修改目錄權限
sudo chown -R 用戶名 目錄path
修改目錄所屬用戶
sudo chgrp -R 用戶組 目錄path
修改目錄所屬用戶組 - 4、SELinux設置為開啟狀態(enabled)的原因
/usr/sbin/sestatus
查看SELinux狀態
將SELINUX=enforcing 修改為 SELINUX=disabled 狀態
臨時修改(重啟失效):
sudo setenforce 0
修改配置文件(永久修改,重啟生效):
vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled
將SELINUX=enforcing 修改為 SELINUX=disabled 狀態
- 1、查看主進程與功能進程是否為同一用戶啟動。