一、ubuntu上安裝配置Nginx:
https://segmentfault.com/a/1190000015797789
目錄
1.安裝
2.配置
3.卸載
4.基本操作總結
基本步驟:
1.更新包管理器后安裝nginx:
sudo apt-get update
sudo apt-get install nginx
2.測試是否安裝成功:
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
3.配置Nginx
最新版本nginx配置是由4個文件構成:
conf.d:用戶自己定義的conf配置文件
sites-available:系統(tǒng)默認設置的配置文件
sites-enabled:由sites-available中的配置文件轉換生成
nginx.conf:匯總以上三個配置文件的內容茄猫,同時配置我們所需要的參數(shù)
在部署需要的web服務時困肩,我們可以拷貝sites-enabled中的default文件到conf.d并且修改名字為**.conf,然后進行配置
server {
#服務啟動時監(jiān)聽的端口
listen 80 default_server;
listen [::]:80 default_server;
#服務啟動時文件加載的路徑
root /var/www/html/wordpress;
#默認加載的第一個文件
index index.php index.html index.htm index.nginx-debian.html;
#頁面訪問域名,如果沒有域名也可以填寫_
server_name www.xiexianbo.xin;
location / {
#頁面加載失敗后所跳轉的頁面
try_files $uri $uri/ =404;
}
#以下配置只服務于php
# 將PHP腳本傳遞給在127.0.0.1:9000上監(jiān)聽的FastCGI服務器
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# 如果Apache的文檔為root勇劣,則拒絕訪問.htaccess文件
location ~ /\.ht {
deny all;
}
}
注意事項:
apache的端口也是80潭枣,所以我們可以選擇關閉apache或者,在這里更換端口盆犁,例如81,82等谐岁,但是我們需要吧這個端口開放出來
React、Vue等由于是單頁面應用窜司,所以我們在刷新的會遇到資源加載不到的錯誤锭魔,這時我們需要把頁面重定向到index.html
try_files $uri /index.html;
每次配置完成后,都需要重啟nginx迷捧。
4.基本操作總結
- 首先利用配置文件啟動nginx。
命令: nginx -c /usr/local/nginx/conf/nginx.conf
重啟服務: service nginx restart- 快速停止或關閉Nginx:nginx -s stop
- 正常停止或關閉Nginx:nginx -s quit
- 配置文件修改重裝載命令:nginx -s reload
二笙蒙、卸載
1庆锦、刪除nginx捅位,-purge包括配置文件
sudo apt-get --purge remove nginx
2、移除全部不使用的軟件包
sudo apt-get autoremove
3尿扯、羅列出與nginx相關的軟件并刪除
dpkg --get-selections|grep nginx
sudo apt-get --purge remove nginx
sudo apt-get --purge remove nginx-common
sudo apt-get --purge remove nginx-core
4焰雕、查看nginx正在運行的進程,如果有就kill掉
ps -ef |grep nginx
sudo kill -9 XXX