合肥個人建站模板廣東省疫情最新
安裝nginx
依次執(zhí)行以下兩條命令進行安裝:
sudo apt-get update
sudo apt-get install nginx
通過查看版本號查看是否安裝成功:
nginx -v
補充卸載操作:
sudo apt-get remove nginx nginx-common
sudo apt-get purge nginx nginx-common
sudo apt-get autoremove
sudo apt-get remove nginx-full nginx-common
補充Ubuntu的包管理器的用法:
sudo apt-get update:更新軟件源
sudo apt-get upgrade:升級系統(tǒng)
sudo apt-get install package_name:安裝軟件包
sudo apt-get remove package_name:卸載軟件包
sudo apt-get autoremove:自動清除無用依賴項
sudo apt-get purge package_name:卸載軟件及其相關配置文件
nginx的配置文件
nginx的配置文件和靜態(tài)資源文件分布位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件(nginx.conf)
/usr/share/nginx:存放靜態(tài)文件
/var/log/nginx:存放日志
本次實驗將配置文件放到/etc/nginx/configs下:
為了操作方便我將切換到了超級用戶:
sudo su
cd /etc/nginx
mkdir configs
最初的/etc/nginx目錄下的文件狀態(tài)如下:
創(chuàng)建文件夾configs用于存放nginx的配置文件:
配置文件相關注釋:
以下是學習項目的一個配置:
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include 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 logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;map $http_upgrade $connection_upgrade{default upgrade;'' close;}upstream webservers{server 127.0.0.1:8080 weight=90 ;#server 127.0.0.1:8088 weight=10 ;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html/sky;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 html;}# 反向代理,處理管理端發(fā)送的請求location /api/ {proxy_pass http://localhost:8080/admin/;#proxy_pass http://webservers/admin/;}# 反向代理,處理用戶端發(fā)送的請求location /user/ {proxy_pass http://webservers/user/;}# WebSocketlocation /ws/ {proxy_pass http://webservers/ws/;proxy_http_version 1.1;proxy_read_timeout 3600s;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "$connection_upgrade";}# 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;#}}# 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;# }#}}
創(chuàng)建并寫入配置文件:
cd configs
touch test.conf
gedit test.conf
將我們添加的配置導入到nginx的配置中:
cd /etc/nginx
vim nginx.conf
添加:include /etc/nginx/configs/*.conf;
nginx相關命令
啟動:
systemctl start nginx
停止:
systemctl stop nginx
重啟:
systemctl reload nginx
查看nginx的啟停狀態(tài):(如果正常啟動,會看到綠色的Runing)
systemctl status nginx
如:
訪問localhost(默認80端口)
部署前端項目
我第二天重啟nginx的時候,發(fā)現(xiàn)它報錯如work_process,events相關的錯誤,應該是configs下的test.conf與/etc/nginx/nginx.conf中的內容發(fā)生了沖突,所以我直接將test.conf的內容(見上面)復制在nginx.conf中,才重啟成功。
路徑相關配置:在配置文件中將以下路徑改為你自己的打包好后的前端代碼所在路徑:
location / {root /path/webcode;index index.html index.htm;}
修改路徑后,重啟nginx。
按理說,這時我們訪問localhost的時候就會訪問前端代碼的index文件。但是我的是403 Forbidden。
我修改了以下兩個配置才能正常訪問:
一、使nginx的啟動用戶和nginx的工作用戶一致:
查看當前nginx的啟動用戶
ps aux|grep nginx
發(fā)現(xiàn)nginx的工作用戶為nobody,所以再次修改配置文件中的啟動文件:
再次查看:
二、修改用戶對前端文件的訪問權限:
chmod -R 755 /path/webcode
最后終于能正常訪問了,暫時就踩了這些坑,完美撒花~