1. 程式人生 > 實用技巧 >Ubuntu-18.04.3安裝nginx即配置

Ubuntu-18.04.3安裝nginx即配置

Nginx

開源,支援高效能,高併發的www服務和代理服務軟體 web伺服器 反向代理伺服器 郵件代理伺服器 負載均衡 動靜分離

編譯安裝nginx

1.解決原始碼編譯安裝nginx所需的軟體依賴

yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

2.下載淘寶nginx的原始碼包

wget http://tengine.taobao.org/download/tengine-2.3.0.tar.gz

3.解壓縮原始碼包

cd ~ #安裝在非root 目錄下,方便後期修改使用

tar -zxvf tengine-2.3.0.tar.gz -C ./

4.進入原始碼目錄開始編譯安裝

cd tengine-2.3.0

編譯安裝三部曲:

./configure --prefix=/home/root123/tengine-2.3.0/nginx --conf-path=/home/root123/tengine-2.3.0/nginx/conf/nginx.conf # 指定配置檔案安裝nginx

make && make install

5.配置nginx環境變數,寫入/etc/profile 或者寫入到 /etc/environment; 前者是加入到所有使用者環境中, 後者是加入到系統中, 即啟動時即載入, 建議編輯vim /etc/environment 檔案

在變數最後配置變數如下內容

PATH="/opt/python362/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/home/root123/tengine-2.3.0/nginx/sbin"

讀取檔案

source /etc/environment

6.啟動nginx

命令:
nginx -c /home/root123/tngx230/nginx/conf/nginx.conf # 指定配置檔案啟動
nginx -s reload  #平滑重啟nginx,不重啟nginx,僅僅是重新讀取nginx配置檔案
nginx -s stop  #停止nginx程序
nginx -t  #檢測nginx.conf的語法

nginx目錄配置檔案資訊

ls /opt/tngx230/

conf 存放nginx配置檔案的
html 存放前端檔案目錄 ,首頁檔案就在這裡
logs 存放nginx執行日誌,錯誤日誌
sbin 存放nginx執行指令碼的

/opt/tngx230/html就是 nginx的網頁根目錄,放入任意的檔案內容都可以被解析到

比如在目錄下放入一張jpg圖片 訪問IP地址會自動訪問顯示這張圖片

nginx配置檔案

#全域性變數寫在最外層
	worker_processes  4;

	events {
		worker_connections  1024;
	}

	#定義nginx核心功能的引數
	http {
		include       mime.types;
		default_type  application/octet-stream;
		#定義nginx訪問日誌格式
		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;
		#對圖片等靜態資源壓縮,提升傳輸效率
		gzip  on;
		#nginx的主頁面功能都是server引數提供的
		#server被稱作是虛擬主機
		server {
			#nginx監聽的埠
			listen       80;
			#填寫伺服器的域名,或者ip,或者localhost
			server_name  localhost;
			#路徑匹配,當你的請求來自於 192.168.16.37/ 這樣的url的時候,就進入以下的location路徑匹配
			location / {
				#root引數,定義網頁根目錄的,nginx目錄下的html,可以自由更改
				root   html;
				#指定首頁檔案的名字的
				index  index.html;
			}
			#錯誤頁面 
			#當請求返回404錯誤碼的時候,就給使用者看到一個頁面
			#這個404.html放在網頁根目錄下
			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;
			}
		}
	}