這篇文章主要介紹“Centos7系統(tǒng)怎么搭建Nginx服務(wù)器”,在日常操作中,相信很多人在Centos7系統(tǒng)怎么搭建Nginx服務(wù)器問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Centos7系統(tǒng)怎么搭建Nginx服務(wù)器”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)專注于平橋網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供平橋營銷型網(wǎng)站建設(shè),平橋網(wǎng)站制作、平橋網(wǎng)頁設(shè)計、平橋網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造平橋網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供平橋網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
下面開始安裝 Nginx:
一、準(zhǔn)備工作:
Centos 7 系統(tǒng)及光盤
編譯安裝的軟件包: https://pan.baidu.com/s/1-GaLSYxt4fP5R2gCVwpILA
提取碼: kph6
也可以從官網(wǎng) https://nginx.org/ 下載使用
二、開始搭建Nginx網(wǎng)站:
安裝所需要的依賴包并卸載當(dāng)前有的 httpd 服務(wù)(如果確定沒有,可省略):
[root@MySQL yum.repos.d]# yum -y erase httpd [root@mysql /]# yum -y install pcre-devel zlib-devel # 安裝所需依賴包
編譯安裝及配置優(yōu)化 Nginx:
[root@mysql /]# useradd -M -s /sbin/nologin nginx # Nginx 默認以 nobody 身份運行,建議創(chuàng)建一個專門的用戶賬號 [root@mysql /]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/ [root@mysql /]# cd /usr/src/nginx-1.12.0/ [root@mysql nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx \ > --group=nginx --with-http_stub_status_module && make && make install [root@mysql /]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ # 創(chuàng)建鏈接文件,方便命令使用
Nginx 運行控制:
1、檢查配置文件
[root@mysql /]# nginx -t # 檢查配置文件,如果出現(xiàn) OK 或 successful 字樣說明沒問題 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
2、啟動、停止 Nginx
[root@mysql /]# nginx # 啟動 [root@mysql /]# killall -s HUP nginx # 重啟 選項 -s HUP 等同于 -1 [root@mysql /]# killall -s QUIT nginx # 關(guān)閉 選項 -s QUIT 等同于 -3
注意:最小化安裝的 centos 7 默認沒有安裝 killall 命令,可以使用 “yum -y install psmisc”
為了使 Nginx 服務(wù)的啟動、停止、重載等操作更方便,可以編寫一個 Nginx 服務(wù)腳本,這亞子更符合 Centos 系統(tǒng)的管理習(xí)慣:
[root@mysql /]# vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "USAGE:$0 {start | stop | restart | reload}" exit 1 esac exit 0 [root@mysql /]# chmod +x /etc/init.d/nginx // 授予權(quán)限 [root@mysql /]# chkconfig --add nginx // 添加為系統(tǒng)服務(wù) [root@mysql /]# systemctl start nginx // 啟動 Nginx
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf ................... #user nobody; // 運行用戶 worker_processes 1; // 工作進程數(shù)量 #error_log logs/error.log; // 錯誤日志文件的位置 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; // PID 文件的位置 events { use epoll; // 使用 epoll 模型,以提高性能 worker_connections 4096; // 每個進程處理 4096 個連接 }
以上的優(yōu)化是基于全局配置實施的,各項優(yōu)化的含義如下:
1、worker_processes :表示工作進程的數(shù)量,若服務(wù)器由多塊CPU或者使用多核處理器,可以參考CPU核心總數(shù)來指定工作進程數(shù)。具體含義在worker_connections配置項中體現(xiàn)出來,
2、worker_connections:這個配置項指定的是每個進程處理的連接,一般在10000以下(默認為1024),與上面工作進程數(shù)量的配置項關(guān)聯(lián),舉個栗子:若工作進程數(shù)為8,每個進程處理4096個連接,則允許Nginx正常提供服務(wù)的連接數(shù)已經(jīng)超過了3萬個(4096*8=32768)。當(dāng)然,具體還要看服務(wù)器硬件、網(wǎng)絡(luò)帶寬等物理條件的性能表現(xiàn)。
搭建基于域名的虛擬 web 主機:
HTTP配置:
Nginx的配置文件使用“http { }”界定標(biāo)記用于設(shè)定HTTP服務(wù)器,包括訪問日志、http端口、網(wǎng)頁目錄、默認字符集、連接保持,以及虛擬web主機、php解析等網(wǎng)站全局設(shè)置,其中大部分包含在子界定標(biāo)記 “ server { }”內(nèi)?!?server { }”代表一個具體的網(wǎng)站設(shè)置。
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf ................... 省略部分內(nèi)容 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; server { // web 服務(wù)的監(jiān)聽配置 listen 80; // 監(jiān)聽地址及端口 server_name www.test1.com; // 網(wǎng)站名稱 FQDN charset koi8-r; // 網(wǎng)頁的默認字符集 access_log logs/host.access.log main; location / { // 根目錄配置 root html; // 網(wǎng)站根目錄的位置,相對于安裝目錄 index index.html index.php; // 默認首頁,索引頁 } error_page 500 502 503 504 /50x.html; // 內(nèi)部錯誤的反饋頁面 location = /50x.html { // 錯誤頁面配置 root html; }
以上配置只是搭建了一個網(wǎng)站服務(wù),若想運行多個,可復(fù)制配置文件最后面提供的模板,粘貼到 “server{ } ”配置上面,因為在配置文件中有太多的 “ { }”,為了避免錯誤,所以才需復(fù)制到原有的 “server{ } ”之上,如下:
[root@mysql /]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.test1.com; charset utf-8; access_log logs/host.access.log main; location / { root /var/www/test1; index index.html index.php; } location /status { stub_status on; access_log off; } } server { listen 80; server_name www.test2.com; charset utf-8; access_log logs/host.access.log main; location / { root /var/www/test2; index index.html index.php; }
虛擬主機到此就配置完成了,然后重啟服務(wù)使配置生效,DNS 自行配置,可參考博文:https://blog.51cto.com/14227204/2384462
[root@mysql /]# nginx -t # 重啟服務(wù)之前,最好是檢測一下 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 [root@mysql /]# systemctl restart nginx [root@mysql named]# mkdir -p /var/www/test1 # 創(chuàng)建網(wǎng)站根目錄 [root@mysql named]# mkdir -p /var/www/test2 [root@mysql named]# echo www.test1.com > /var/www/test1/index.html # 創(chuàng)建測試文件 [root@mysql named]# echo www.test2.com > /var/www/test2/index.html
客戶機開始驗證:
查看當(dāng)前的狀態(tài)信息:
測試另一個域名:
到此,關(guān)于“Centos7系統(tǒng)怎么搭建Nginx服務(wù)器”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站欄目:Centos7系統(tǒng)怎么搭建Nginx服務(wù)器
分享地址:http://jinyejixie.com/article12/ipegdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航、虛擬主機、企業(yè)網(wǎng)站制作、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)