如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

摘要:Nginx 是最快和最強(qiáng)大的 Web 服務(wù)器之一,以其高性能和低資源占用率而聞名。它既可以被安裝為一個獨立的 Web 服務(wù)器,也可以安裝成反向代理 Web 服務(wù)器。在這篇文章,我將討論在安裝了 cPanel 管理系統(tǒng)的 Centos 7 服務(wù)器上安裝 Nginx 作為 Apache 的反向代理服務(wù)器。

Nginx 是最快和最強(qiáng)大的 Web 服務(wù)器之一,以其高性能和低資源占用率而聞名。它既可以被安裝為一個獨立的 Web 服務(wù)器,也可以安裝成反向代理 Web 服務(wù)器。在這篇文章,我將討論在安裝了 cPanel 管理系統(tǒng)的 Centos 7 服務(wù)器上安裝 Nginx 作為 Apache 的反向代理服務(wù)器。

Nginx 作為前端服務(wù)器用反向代理為靜態(tài)文件提供服務(wù),Apache 作為后端為動態(tài)文件提供服務(wù)。這個設(shè)置將整體提高服務(wù)器的性能。

讓我們過一遍在已經(jīng)安裝好 cPanel 11.52 的 CentOS 7 x86_64 服務(wù)器上配置 Nginx 作為反向代理的安裝過程。

首先,我們需要安裝 EPEL 庫來啟動這個進(jìn)程

第一步: 安裝 EPEL 庫

root@server1[/usr]#yum-y install epel-release

Loadedplugins:fastestmirror,tsflags,universal-hooks

Loadingmirror speedsfromcached hostfile

*EA4:66.23.237.210

*base:mirrors.linode.com

*extras:mirrors.linode.com

*updates:mirrors.linode.com

ResolvingDependencies

-->Runningtransaction check

--->Packageepel-release.noarch0:7-5will be installed

-->FinishedDependencyResolution

DependenciesResolved

========================================================================================

PackageArchVersionRepositorySize

========================================================================================

Installing:

epel-release noarch7-5extras14k

第二步: 可以安裝 nDeploy 的 CentOS RPM 庫來安裝我們所需的 nDeploy Web 類軟件和 Nginx 插件

root@server1[/usr]# yum -y install http://rpm.piserve.com/nDeploy-release-centos-1.0-1.noarch.rpm

Loadedplugins:fastestmirror,tsflags,universal-hooks

nDeploy-release-centos-1.0-1.noarch.rpm|1.7kB00:00:00

Examining/var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm:nDeploy-release-centos-1.0-1.noarch

Marking/var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm to be installed

ResolvingDependencies

-->Runningtransaction check

--->PackagenDeploy-release-centos.noarch0:1.0-1will be installed

-->FinishedDependencyResolution

DependenciesResolved

========================================================================================

PackageArchVersionRepositorySize

========================================================================================

Installing:

nDeploy-release-centos noarch1.0-1/nDeploy-release-centos-1.0-1.noarch110

第三步:安裝 nDeploy 和 Nginx nDeploy 插件

root@server1[/usr]#yum--enablerepo=ndeploy install nginx-nDeploy nDeploy

Loadedplugins:fastestmirror,tsflags,universal-hooks

epel/x86_64/metalink|9.9kB00:00:00

epel|4.3kB00:00:00

ndeploy|2.9kB00:00:00

(1/4):ndeploy/7/x86_64/primary_db|14kB00:00:00

(2/4):epel/x86_64/group_gz|169kB00:00:00

(3/4):epel/x86_64/primary_db|3.7MB00:00:02

DependenciesResolved

========================================================================================

PackageArchVersionRepositorySize

========================================================================================

Installing:

nDeploy noarch2.0-11.el7ndeploy80k

nginx-nDeploy x86_641.8.0-34.el7ndeploy36M

Installingfordependencies:

PyYAMLx86_643.10-11.el7base153k

libevent x86_642.0.21-4.el7base214k

memcached x86_641.4.15-9.el7base84k

python-inotify noarch0.9.4-4.el7base49k

python-lxml x86_643.2.1-4.el7base758k

TransactionSummary

========================================================================================

Install2Packages(+5Dependentpackages)

通過以上這些步驟,我們完成了在我們的服務(wù)器上 Nginx 插件的安裝?,F(xiàn)在我們可以配置 Nginx 作為反向代理和為已有的 cPanel 用戶賬戶創(chuàng)建虛擬主機(jī),為此我們可以運(yùn)行如下腳本。

第四步:啟動 Nginx 作為默認(rèn)的前端 Web 服務(wù)器,并創(chuàng)建默認(rèn)的配置文件

root@server1[/usr]# /opt/nDeploy/scripts/cpanel-nDeploy-setup.sh enable

Modifyingapache httpandhttps portincpanel

httpd restarted successfully.

Createdsymlinkfrom/etc/systemd/system/multi-user.target.wants/nginx.service to/usr/lib/systemd/system/nginx.service.

Createdsymlinkfrom/etc/systemd/system/multi-user.target.wants/ndeploy_watcher.service to/usr/lib/systemd/system/ndeploy_watcher.service.

Createdsymlinkfrom/etc/systemd/system/multi-user.target.wants/ndeploy_backends.service to/usr/lib/systemd/system/ndeploy_backends.service.

ConfGen::saheetha

ConfGen::satest

你可以看到這個腳本將修改 Apache 的端口從 80 到另一個端口來讓 Nginx 作為前端 Web 服務(wù)器,并為現(xiàn)有的 cPanel 用戶創(chuàng)建虛擬主機(jī)配置文件。一旦完成,確認(rèn) Apache 和 Nginx 的狀態(tài)。

Apache 狀態(tài):

root@server1[/var/run/httpd]#systemctlstatus httpd

●httpd.service-ApacheWebServer

Loaded:loaded(/usr/lib/systemd/system/httpd.service;enabled;vendor preset:disabled)

Active:active(running)sinceMon2016-01-1806:34:23UTC;12sago

Process:25606ExecStart=/usr/sbin/apachectl start(code=exited,status=0/SUCCESS)

MainPID:24760(httpd)

CGroup:/system.slice/httpd.service

?24760/usr/local/apache/bin/httpd-k start

Jan1806:34:23server1.centos7-test.comsystemd[1]:StartingApacheWebServer...

Jan1806:34:23server1.centos7-test.com apachectl[25606]:httpd(pid24760)already running

Jan1806:34:23server1.centos7-test.comsystemd[1]:StartedApacheWebServer.

Nginx 狀態(tài):

root@server1[~]#systemctlstatus nginx

●nginx.service-nginx-nDeploy-high performance web server

Loaded:loaded(/usr/lib/systemd/system/nginx.service;enabled;vendor preset:disabled)

Active:active(running)sinceSun2016-01-1717:18:29UTC;13hago

Docs:http://nginx.org/en/docs/

MainPID:3833(nginx)

CGroup:/system.slice/nginx.service

├─3833nginx:master process/usr/sbin/nginx-c/etc/nginx/nginx.conf

├─25473nginx:worker process

├─25474nginx:worker process

└─25475nginx:cache manager process

Jan1717:18:29server1.centos7-test.comsystemd[1]:Startingnginx-nDeploy-high performance web server...

Jan1717:18:29server1.centos7-test.com nginx[3804]:nginx:the configurationfile/etc/nginx/nginx.conf syntaxisok

Jan1717:18:29server1.centos7-test.com nginx[3804]:nginx:configurationfile/etc/nginx/nginx.conftestissuccessful

Jan1717:18:29server1.centos7-test.comsystemd[1]:Startednginx-nDeploy-high performance web server.

Nginx 作為前端服務(wù)器運(yùn)行在 80 端口,Apache 配置被更改為監(jiān)聽 http 端口 9999 和 https 端口 4430。請看他們的情況:

root@server1[/usr/local/src]#netstat-plan|grephttpd

tcp000.0.0.0:44300.0.0.0:*LISTEN17270/httpd

tcp000.0.0.0:99990.0.0.0:*LISTEN17270/httpd

tcp600:::4430:::*LISTEN17270/httpd

tcp600:::9999:::*LISTEN17270/httpd

apacheport

root@server1[/usr/local/src]#netstat-plan|grepnginx

tcp00127.0.0.1:800.0.0.0:*LISTEN17802/nginx:master

tcp0045.79.183.73:800.0.0.0:*LISTEN17802/nginx:master

為已有用戶創(chuàng)建的虛擬主機(jī)的配置文件在 “/etc/nginx/sites-enabled”。 這個文件路徑包含了 Nginx 主要配置文件。

root@server1[/etc/nginx/sites-enabled]#ll|grep.conf

-rw-r--r--1root root311Jan1709:02saheetha.com.conf

-rw-r--r--1root root336Jan1709:02saheethastest.com.conf

一個域名的示例虛擬主機(jī):

server{

listen45.79.183.73:80;

#CPIPVSIX:80;

#ServerNames

server_name saheetha.com www.saheetha.com;

access_log/usr/local/apache/domlogs/saheetha.com main;

access_log/usr/local/apache/domlogs/saheetha.com-bytes_log bytes_log;

include/etc/nginx/sites-enabled/saheetha.com.include;

}

我們可以啟動瀏覽器查看網(wǎng)站來確定 Web 服務(wù)器的工作狀態(tài)。安裝后,請閱讀服務(wù)器上的 web 服務(wù)信息。

root@server1[/home]#ipa|grep-i eth0

3:eth0:mtu1500qdisc pfifo_fast state UP qlen1000

inet45.79.183.73/24brd45.79.183.255scopeglobaldynamic eth0

root@server1[/home]#nginx-v

nginx version:nginx/1.8.0

webserver-status

Nginx 將會為任何最新在 cPanel 中創(chuàng)建的賬戶創(chuàng)建虛擬主機(jī)。通過這些簡單的的步驟,我們能夠在一臺 CentOS 7 / cPanel 的服務(wù)器上配置 Nginx 作為反向代理。

Nginx 作為反向代理的優(yōu)勢

便于安裝和配置。

效率高、性能好。

防止 Ddos 攻擊。

支持使用 .htaccess 作為 PHP 的重寫規(guī)則。

原文發(fā)布時間為:2016-09-07

本文來自云棲社區(qū)合作伙伴“Linux中國”

原文鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容