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

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

[[171184]]

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

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

[[171185]]

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

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

第一步: 安装 EPEL 库

  1. root@server1 [/usr]# yum -y install epel-release 
  2. Loaded plugins: fastestmirror, tsflags, universal-hooks 
  3. Loading mirror speeds from cached hostfile 
  4. * EA4: 66.23.237.210 
  5. * base: mirrors.linode.com 
  6. * extras: mirrors.linode.com 
  7. * updates: mirrors.linode.com 
  8. Resolving Dependencies 
  9. --> Running transaction check 
  10. ---> Package epel-release.noarch 0:7-5 will be installed 
  11. --> Finished Dependency Resolution 
  12. Dependencies Resolved 
  13. ======================================================================================== 
  14.     Package Arch Version Repository Size 
  15. ======================================================================================== 
  16. Installing: 
  17. epel-release noarch 7-5 extras 14 k 

第二步: 可以安装 nDeploy 的 CentOS RPM 库来安装我们所需的 nDeploy Web 类软件和 Nginx 插件

  1. root@server1 [/usr]# yum -y install http://rpm.piserve.com/nDeploy-release-centos-1.0-1.noarch.rpm 
  2. Loaded plugins: fastestmirror, tsflags, universal-hooks 
  3. nDeploy-release-centos-1.0-1.noarch.rpm | 1.7 kB 00:00:00 
  4. Examining /var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm: nDeploy-release-centos-1.0-1.noarch 
  5. Marking /var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm to be installed 
  6. Resolving Dependencies 
  7. --> Running transaction check 
  8. ---> Package nDeploy-release-centos.noarch 0:1.0-1 will be installed 
  9. --> Finished Dependency Resolution 
  10. Dependencies Resolved 
  11. ======================================================================================== 
  12. Package Arch Version Repository Size 
  13. ======================================================================================== 
  14. Installing: 
  15. nDeploy-release-centos noarch 1.0-1 /nDeploy-release-centos-1.0-1.noarch 110 

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

  1. root@server1 [/usr]# yum --enablerepo=ndeploy install nginx-nDeploy nDeploy 
  2. Loaded plugins: fastestmirror, tsflags, universal-hooks 
  3. epel/x86_64/metalink | 9.9 kB 00:00:00 
  4. epel | 4.3 kB 00:00:00 
  5. ndeploy | 2.9 kB 00:00:00 
  6. (1/4): ndeploy/7/x86_64/primary_db | 14 kB 00:00:00 
  7. (2/4): epel/x86_64/group_gz | 169 kB 00:00:00 
  8. (3/4): epel/x86_64/primary_db | 3.7 MB 00:00:02 
  9. Dependencies Resolved 
  10. ======================================================================================== 
  11. Package Arch Version Repository Size 
  12. ======================================================================================== 
  13. Installing: 
  14. nDeploy noarch 2.0-11.el7 ndeploy 80 k 
  15. nginx-nDeploy x86_64 1.8.0-34.el7 ndeploy 36 M 
  16. Installing for dependencies: 
  17. PyYAML x86_64 3.10-11.el7 base 153 k 
  18. libevent x86_64 2.0.21-4.el7 base 214 k 
  19. memcached x86_64 1.4.15-9.el7 base 84 k 
  20. python-inotify noarch 0.9.4-4.el7 base 49 k 
  21. python-lxml x86_64 3.2.1-4.el7 base 758 k 
  22. Transaction Summary 
  23. ======================================================================================== 
  24. Install 2 Packages (+5 Dependent packages) 

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

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

  1. root@server1 [/usr]# /opt/nDeploy/scripts/cpanel-nDeploy-setup.sh enable 
  2. Modifying apache http and https port in cpanel 
  3. httpd restarted successfully. 
  4. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 
  5. Created symlink from /etc/systemd/system/multi-user.target.wants/ndeploy_watcher.service to /usr/lib/systemd/system/ndeploy_watcher.service. 
  6. Created symlink from /etc/systemd/system/multi-user.target.wants/ndeploy_backends.service to /usr/lib/systemd/system/ndeploy_backends.service. 
  7. ConfGen:: saheetha 
  8. ConfGen:: satest 

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

Apache 状态: 

  1. root@server1 [/var/run/httpd]# systemctl status httpd 
  2. ● httpd.service - Apache Web Server 
  3. Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) 
  4. Active: active (running) since Mon 2016-01-18 06:34:23 UTC; 12s ago 
  5. Process: 25606 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) 
  6. Main PID: 24760 (httpd) 
  7. CGroup: /system.slice/httpd.service 
  8. ‣ 24760 /usr/local/apache/bin/httpd -k start 
  9. Jan 18 06:34:23 server1.centos7-test.com systemd[1]: Starting Apache Web Server... 
  10. Jan 18 06:34:23 server1.centos7-test.com apachectl[25606]: httpd (pid 24760) already running 
  11. Jan 18 06:34:23 server1.centos7-test.com systemd[1]: Started Apache Web Server. 

Nginx 状态:

  1. root@server1 [~]# systemctl status nginx 
  2. ● nginx.service - nginx-nDeploy - high performance web server 
  3. Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) 
  4. Active: active (running) since Sun 2016-01-17 17:18:29 UTC; 13h ago 
  5. Docs: http://nginx.org/en/docs/ 
  6. Main PID: 3833 (nginx) 
  7. CGroup: /system.slice/nginx.service 
  8. ├─ 3833 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 
  9. ├─25473 nginx: worker process 
  10. ├─25474 nginx: worker process 
  11. └─25475 nginx: cache manager process 
  12. Jan 17 17:18:29 server1.centos7-test.com systemd[1]: Starting nginx-nDeploy - high performance web server... 
  13. Jan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
  14. Jan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: configuration file /etc/nginx/nginx.conf test is successful 
  15. Jan 17 17:18:29 server1.centos7-test.com systemd[1]: Started nginx-nDeploy - high performance web server. 

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

  1. root@server1 [/usr/local/src]# netstat -plan | grep httpd 
  2. tcp 0 0 0.0.0.0:4430 0.0.0.0:* LISTEN 17270/httpd 
  3. tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 17270/httpd 
  4. tcp6 0 0 :::4430 :::* LISTEN 17270/httpd 
  5. tcp6 0 0 :::9999 :::* LISTEN 17270/httpd 

 

 apacheport 

  1. root@server1 [/usr/local/src]# netstat -plan | grep nginx 
  2. tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN 17802/nginx: master 
  3. tcp 0 0 45.79.183.73:80 0.0.0.0:* LISTEN 17802/nginx: master 

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

  1. root@server1 [/etc/nginx/sites-enabled]# ll | grep .conf 
  2. -rw-r--r-- 1 root root 311 Jan 17 09:02 saheetha.com.conf 
  3. -rw-r--r-- 1 root root 336 Jan 17 09:02 saheethastest.com.conf 

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

  1. server { 
  2. listen 45.79.183.73:80; 
  3. #CPIPVSIX:80; 
  4. # ServerNames 
  5. server_name saheetha.com www.saheetha.com; 
  6. access_log /usr/local/apache/domlogs/saheetha.com main; 
  7. access_log /usr/local/apache/domlogs/saheetha.com-bytes_log bytes_log; 
  8. include /etc/nginx/sites-enabled/saheetha.com.include; 

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

  1. root@server1 [/home]# ip a | grep -i eth0 
  2. 3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 
  3. inet 45.79.183.73/24 brd 45.79.183.255 scope global dynamic eth0 
  4. root@server1 [/home]# nginx -v 
  5. nginx version: nginx/1.8.0 

 

 webserver-status

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

Nginx 作为反向代理的优势

  1. 便于安装和配置。
  2. 效率高、性能好。
  3. 防止 Ddos 攻击。
  4. 支持使用 .htaccess 作为 PHP 的重写规则。

我希望这篇文章对你们有用。感谢你看它。我非常高兴收到你的宝贵意见和建议,并进一步改善。

 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2022-07-01 07:33:24

nginx反向代理测试

2011-08-30 11:32:53

UbuntuNginx

2014-04-29 14:54:48

Nginx反向代理

2017-09-06 10:14:29

Nginx TCPmail邮件

2023-09-13 07:16:31

Ngnix代理服务器

2016-01-08 10:37:56

FreeBSD 10.Nginx反向代理

2012-12-07 10:14:48

Nginx负载均衡

2023-12-05 09:14:54

2023-10-17 08:36:28

Nginx代理服务器

2020-10-22 08:05:46

Nginx

2019-07-09 15:10:02

Nginx反向代理负载均衡

2019-06-19 15:34:39

Nginx反向代理负载均衡

2018-11-12 12:17:00

2019-05-22 14:42:43

SSLNginx反向代理

2010-03-30 14:35:58

Nginx反向代理

2020-08-06 08:23:24

Nginx反向代理Web安全

2018-12-24 08:48:14

NginxApache Cookie

2017-12-18 12:04:02

Nginx代理均衡

2019-11-04 15:35:53

Nginx反向代理负载均衡

2019-09-18 10:39:08

负载均衡反向代理TCP
点赞
收藏

51CTO技术栈公众号