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

摘要:Nginx 是最快和最强大的 Web 服务器之一,以其高性能和低资源占用率而闻名。它既可以被安装为一个独立的 Web 服务器,也可以安装成反向代理 Web 服务器。在这篇文章,我将讨论在安装了 cPanel 管理系统的 Centos 7 服务器上安装 Nginx 作为 Apache 的反向代理服务器。

Nginx 是最快和最强大的 Web 服务器之一,以其高性能和低资源占用率而闻名。它既可以被安装为一个独立的 Web 服务器,也可以安装成反向代理 Web 服务器。在这篇文章,我将讨论在安装了 cPanel 管理系统的 Centos 7 服务器上安装 Nginx 作为 Apache 的反向代理服务器。

Nginx 作为前端服务器用反向代理为静态文件提供服务,Apache 作为后端为动态文件提供服务。这个设置将整体提高服务器的性能。

让我们过一遍在已经安装好 cPanel 11.52 的 CentOS 7 x86_64 服务器上配置 Nginx 作为反向代理的安装过程。

首先,我们需要安装 EPEL 库来启动这个进程

第一步: 安装 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)

通过以上这些步骤,我们完成了在我们的服务器上 Nginx 插件的安装。现在我们可以配置 Nginx 作为反向代理和为已有的 cPanel 用户账户创建虚拟主机,为此我们可以运行如下脚本。

第四步:启动 Nginx 作为默认的前端 Web 服务器,并创建默认的配置文件

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 服务器,并为现有的 cPanel 用户创建虚拟主机配置文件。一旦完成,确认 Apache 和 Nginx 的状态。

Apache 状态:

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 状态:

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 作为前端服务器运行在 80 端口,Apache 配置被更改为监听 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

为已有用户创建的虚拟主机的配置文件在 “/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

一个域名的示例虚拟主机:

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;

}

我们可以启动浏览器查看网站来确定 Web 服务器的工作状态。安装后,请阅读服务器上的 web 服务信息。

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 中创建的账户创建虚拟主机。通过这些简单的的步骤,我们能够在一台 CentOS 7 / cPanel 的服务器上配置 Nginx 作为反向代理。

Nginx 作为反向代理的优势

便于安装和配置。

效率高、性能好。

防止 Ddos 攻击。

支持使用 .htaccess 作为 PHP 的重写规则。

原文发布时间为:2016-09-07

本文来自云栖社区合作伙伴“Linux中国”

原文链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容