前端部署nginx伺服器

2022-07-27 12:33:12 字數 3648 閱讀 1727

linux版本:centos7 64位

(1)安裝前先檢視nginx有無安裝:yum list | grep nginx,或者yum list installed | grep "nginx",如果已經安裝了,則會出現@的標識

未安裝情況下:

已安裝情況下:

(2)安裝nginx:yum install nginx(手動選擇),或者yum -y install nginx(自動選擇,跳過is this ok[y/d/n),然後nginx -v,檢視版本號

(3)相關資料夾檢視:rpm -ql nginx,檢視 nginx 被安裝到了什麼地方,有哪些相關目錄,其中位於/etc目錄下的主要是配置檔案,還有一些檔案見下圖:

(4)主要關注的資料夾有兩個:

/etc/nginx/conf.d/資料夾,是我們進行子配置的配置項存放處,/etc/nginx/nginx.conf主配置檔案會預設把這個資料夾中所有子配置項都引入;/usr/share/nginx/html/資料夾,通常靜態檔案都放在這個資料夾,也可以根據你自己的習慣放其他地方;

(5)安裝之後開啟 nginx,如果系統開啟了防火牆,那麼需要設定一下在防火牆中加入需要開放的埠,下面列舉幾個常用的防火牆操作(沒開啟的話不用管這個):

systemctl start firewalld # 開啟防火牆

systemctl stop firewalld # 關閉防火牆

systemctl status firewalld # 檢視防火牆開啟狀態,顯示running則是正在執行

firewall-cmd --reload # 重啟防火牆,永久開啟埠需要reload一下

# 新增開啟埠,--permanent表示永久開啟,不加是臨時開啟重啟之後失效

firewall-cmd --permanent --zone=public --add-port=8888/tcp

# 檢視防火牆,新增的埠也可以看到

firewall-cmd --list-all

(6)設定 nginx 的開機啟動:systemctl enable nginx

(7)啟動 nginx :systemctl start nginx,然後訪問你的 ip,這時候就可以看到 nginx 的歡迎頁面了~welcome to nginx!

(8)nginx操作常用命令:

(9)systemctl是 linux 系統應用管理工具systemd的主命令,用於管理系統,我們也可以用它來對 nginx 進行管理,相關命令如下:

(10)解除安裝nginx:

停止nginx:service nginx stop

刪除nginx的自動啟動:chkconfig nginx off

刪除nginx本地檔案:

rm -rf /usr/sbin/nginx

rm -rf /etc/nginx

rm -rf /etc/init.d/nginx

yum清理:yum remove ngin

(1)在安裝nginx前首先要確認系統中安裝了gcc、pcre-devel、zlib-devel、openssl-devel,

安裝命令:

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

## 解壓

tar -zxvf nginx-1.9.9.tar.gz

##進入nginx目錄

cd nginx-1.9.9

## 配置

./configure --prefix=nginx安裝路徑 --with-http_ssl_module

例如:./configure --prefix=/usr/local/nginx

## 安裝

make

make install

或者:make && make install

(3)測試是否安裝成功

# cd到剛才配置的安裝目錄/usr/loca/nginx/
./sbin/nginx -t
錯誤資訊

nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: no such file or directory)

2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: no such file or directory)

原因分析:nginx/目錄下沒有logs資料夾

解決方法

mkdir logs

chmod 700 logs

正常情況的資訊輸出:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

啟動nginx 

cd /usr/local/nginx/sbin

./nginx //啟動nginx

(5)如果nginx安裝目錄與配置目錄不在乙個地方,則啟動時用下面的命令:

啟動**格式:nginx安裝目錄位址 -c nginx配置檔案位址

例如:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

(6)驗證nginx配置檔案是否正確

方法一:進入nginx安裝目錄 /usr/local/nginx/sbin/ 下,輸入命令./nginx -t看到如下顯示

nginx.conf syntax is ok

nginx.conf test is successful

方法二:在啟動命令-c前加-t,例如:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

(7)重啟nginx服務

進入nginx可執行目錄 /usr/local/nginx/sbin/ 下,輸入命令./nginx -s reload即可

(8)強制停止pkill -9 nginx

前端專案單獨部署nginx伺服器

最近公司有新專案計畫做前後端分離部署,讓前端單獨部署在一台aws的ec2伺服器,記錄一下部署的過程,免得下次又得查閱資料 aws ec2伺服器搭建nginx前端伺服器 nginx前端專案預設存放目錄 usr share nginx html,不想放這裡就在自定義的conf檔案修改專案路徑 這裡的檔案...

Nginx伺服器部署

採用nginx原始碼安裝方式 1.將nginx 1.8.0.tar.gz上傳到 usr local 目錄下 tar zxvf nginx 1.8.0.tar.gz2.進入根目錄執行編譯檢查依賴環境是否滿足.configure cd usr local nginx 1.8.0 configure由於n...

伺服器安裝部署nginx

1 openssl安裝 2 pcre安裝 cd 回到 nginx目錄下 3 zlib安裝 cd 回到 nginx目錄下 4 nginx安裝 cd 回到 nginx目錄下 先找一下nginx安裝到什麼位置上了 whereis nginx cd usr local nginx 這是上一步的結果 進入ng...