天天看点

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

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

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

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

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

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

<a target="_blank"></a>

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

<code>loaded plugins: fastestmirror, tsflags, universal-hooks</code>

<code>loading mirror speeds from cached hostfile</code>

<code>* ea4: 66.23.237.210</code>

<code>* base: mirrors.linode.com</code>

<code>* extras: mirrors.linode.com</code>

<code>* updates: mirrors.linode.com</code>

<code>resolving dependencies</code>

<code>--&gt; running transaction check</code>

<code>---&gt; package epel-release.noarch 0:7-5 will be installed</code>

<code>--&gt; finished dependency resolution</code>

<code></code>

<code>dependencies resolved</code>

<code>========================================================================================</code>

<code>package arch version repository size</code>

<code>installing:</code>

<code>epel-release noarch 7-5 extras 14 k</code>

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

<code>ndeploy-release-centos-1.0-1.noarch.rpm | 1.7 kb 00:00:00</code>

<code>examining /var/tmp/yum-root-ei5twj/ndeploy-release-centos-1.0-1.noarch.rpm: ndeploy-release-centos-1.0-1.noarch</code>

<code>marking /var/tmp/yum-root-ei5twj/ndeploy-release-centos-1.0-1.noarch.rpm to be installed</code>

<code>---&gt; package ndeploy-release-centos.noarch 0:1.0-1 will be installed</code>

<code>ndeploy-release-centos noarch 1.0-1 /ndeploy-release-centos-1.0-1.noarch 110</code>

<code>root@server1 [/usr]# yum --enablerepo=ndeploy install nginx-ndeploy ndeploy</code>

<code>epel/x86_64/metalink | 9.9 kb 00:00:00</code>

<code>epel | 4.3 kb 00:00:00</code>

<code>ndeploy | 2.9 kb 00:00:00</code>

<code>(1/4): ndeploy/7/x86_64/primary_db | 14 kb 00:00:00</code>

<code>(2/4): epel/x86_64/group_gz | 169 kb 00:00:00</code>

<code>(3/4): epel/x86_64/primary_db | 3.7 mb 00:00:02</code>

<code>ndeploy noarch 2.0-11.el7 ndeploy 80 k</code>

<code>nginx-ndeploy x86_64 1.8.0-34.el7 ndeploy 36 m</code>

<code>installing for dependencies:</code>

<code>pyyaml x86_64 3.10-11.el7 base 153 k</code>

<code>libevent x86_64 2.0.21-4.el7 base 214 k</code>

<code>memcached x86_64 1.4.15-9.el7 base 84 k</code>

<code>python-inotify noarch 0.9.4-4.el7 base 49 k</code>

<code>python-lxml x86_64 3.2.1-4.el7 base 758 k</code>

<code>transaction summary</code>

<code>install 2 packages (+5 dependent packages)</code>

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

<code>root@server1 [/usr]# /opt/ndeploy/scripts/cpanel-ndeploy-setup.sh enable</code>

<code>modifying apache http and https port in cpanel</code>

<code>httpd restarted successfully.</code>

<code>created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.</code>

<code>created symlink from /etc/systemd/system/multi-user.target.wants/ndeploy_watcher.service to /usr/lib/systemd/system/ndeploy_watcher.service.</code>

<code>created symlink from /etc/systemd/system/multi-user.target.wants/ndeploy_backends.service to /usr/lib/systemd/system/ndeploy_backends.service.</code>

<code>confgen:: saheetha</code>

<code>confgen:: satest</code>

你可以看到这个脚本将修改 apache 的端口从 80 到另一个端口来让 nginx 作为前端 web 服务器,并为现有的 cpanel 用户创建虚拟主机配置文件。一旦完成,确认 apache 和 nginx 的状态。

<code>root@server1 [/var/run/httpd]# systemctl status httpd</code>

<code>● httpd.service - apache web server</code>

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

<code>active: active (running) since mon 2016-01-18 06:34:23 utc; 12s ago</code>

<code>process: 25606 execstart=/usr/sbin/apachectl start (code=exited, status=0/success)</code>

<code>main pid: 24760 (httpd)</code>

<code>cgroup: /system.slice/httpd.service</code>

<code>‣ 24760 /usr/local/apache/bin/httpd -k start</code>

<code>jan 18 06:34:23 server1.centos7-test.com systemd[1]: starting apache web server...</code>

<code>jan 18 06:34:23 server1.centos7-test.com apachectl[25606]: httpd (pid 24760) already running</code>

<code>jan 18 06:34:23 server1.centos7-test.com systemd[1]: started apache web server.</code>

<code>root@server1 [~]# systemctl status nginx</code>

<code>● nginx.service - nginx-ndeploy - high performance web server</code>

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

<code>active: active (running) since sun 2016-01-17 17:18:29 utc; 13h ago</code>

<code>docs: http://nginx.org/en/docs/</code>

<code>main pid: 3833 (nginx)</code>

<code>cgroup: /system.slice/nginx.service</code>

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

<code>├─25473 nginx: worker process</code>

<code>├─25474 nginx: worker process</code>

<code>└─25475 nginx: cache manager process</code>

<code>jan 17 17:18:29 server1.centos7-test.com systemd[1]: starting nginx-ndeploy - high performance web server...</code>

<code>jan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok</code>

<code>jan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: configuration file /etc/nginx/nginx.conf test is successful</code>

<code>jan 17 17:18:29 server1.centos7-test.com systemd[1]: started nginx-ndeploy - high performance web server.</code>

nginx 作为前端服务器运行在 80 端口,apache 配置被更改为监听 http 端口 9999 和 https 端口 4430。请看他们的情况:

<code>root@server1 [/usr/local/src]# netstat -plan | grep httpd</code>

<code>tcp 0 0 0.0.0.0:4430 0.0.0.0:* listen 17270/httpd</code>

<code>tcp 0 0 0.0.0.0:9999 0.0.0.0:* listen 17270/httpd</code>

<code>tcp6 0 0 :::4430 :::* listen 17270/httpd</code>

<code>tcp6 0 0 :::9999 :::* listen 17270/httpd</code>

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

apacheport

<code>root@server1 [/usr/local/src]# netstat -plan | grep nginx</code>

<code>tcp 0 0 127.0.0.1:80 0.0.0.0:* listen 17802/nginx: master</code>

<code>tcp 0 0 45.79.183.73:80 0.0.0.0:* listen 17802/nginx: master</code>

为已有用户创建的虚拟主机的配置文件在 “/etc/nginx/sites-enabled”。 这个文件路径包含了 nginx 主要配置文件。

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

<code>-rw-r--r-- 1 root root 311 jan 17 09:02 saheetha.com.conf</code>

<code>-rw-r--r-- 1 root root 336 jan 17 09:02 saheethastest.com.conf</code>

<code>server {</code>

<code>listen 45.79.183.73:80;</code>

<code>#cpipvsix:80;</code>

<code># servernames</code>

<code>server_name saheetha.com www.saheetha.com;</code>

<code>access_log /usr/local/apache/domlogs/saheetha.com main;</code>

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

<code>include /etc/nginx/sites-enabled/saheetha.com.include;</code>

<code>}</code>

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

<code>root@server1 [/home]# ip a | grep -i eth0</code>

<code>3: eth0: &lt;broadcast,multicast,up,lower_up&gt; mtu 1500 qdisc pfifo_fast state up qlen 1000</code>

<code>inet 45.79.183.73/24 brd 45.79.183.255 scope global dynamic eth0</code>

<code>root@server1 [/home]# nginx -v</code>

<code>nginx version: nginx/1.8.0</code>

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

webserver-status

nginx 将会为任何最新在 cpanel 中创建的账户创建虚拟主机。通过这些简单的的步骤,我们能够在一台 centos 7 / cpanel 的服务器上配置 nginx 作为反向代理。

便于安装和配置。

效率高、性能好。

防止 ddos 攻击。

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

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

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