第1步:安装apache2
首先运行以下命令以安装apache2 webserver。
sudo apt-get update sudo apt-get install apache2
安装Apache2后,可以使用下面命令管理apache2服务
sudo systemctl stop apache2.service sudo systemctl start apache2.service sudo systemctl enable apache2.service
默认情况下,Apache2 HTTP服务自动绑定到80端口,而HTTPS绑定到443端口.
Varnish是一个开源HTTP加速器。
它通常配置在Web服务器点断,以快速提供HTTP/HTTPS请求。它可以大大提高服务器性能。
这是因为Varnish存储系统内存中的Web缓存,确保在之后的同一资源的请求中更快地检索。
Varnish也可以用作负载均衡器来分发多个WebServers的负载。
本文将介绍如何在Ubuntu 16.04 LTS上 安装和配置Apache2和Varnish。
第4步:配置Varnish使用端口80
现在端口80是空闲的,我们把它给Varnish使用。
varnish默认配置文件是 /etc/default/varnish
通过运行以下命令打开它:
sudo nano /etc/default/varnish
修改DAEMON_OPTS中的端口号:
## Alternative 2, Configuration with VCL ## Listen on port 6081, administration on localhost:6082, and forward to # one content server selected by the vcl file, based on the request. # DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
完成后保存文件。
接下来,运行以下命令以打开default.vcl文件
sudo nano /etc/varnish/default.vcl
设置backend块
# Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; }
保存文件并关闭。
之后,重新启动Apache2和Varnish服务
sudo systemctl restart apache2.service sudo systemctl restart varnish.service
接下来,如果无法启动,则运行以下命令以启动Varnish。
sudo /usr/sbin/varnishd -a :80 -b localhost:8080
如果正确设置了所有内容,则Varnish应为端口80的默认侦听器。
运行以下命令进行测试。
curl -I http://localhost
结果应该是类似下面:
HTTP/1.1 200 OK Date: Sun, 23 Jan 2015 17:45:49 GMT Server: Apache/2.4.25 (Ubuntu) Last-Modified: Sun, 23 Jan 2015 17:01:05 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 10 3 Age: 9 Via: 1.1 varnish (Varnish/5.0) ETag: W/"2aa6-554ff0b3c88c9-gzip" Accept-Ranges: bytes Connection: keep-alive
第3步:将Apache2的默认端口修改为8080
由于我们希望Varnish倾听到端口80(Apache2的默认端口)的所有流量,
所以我们需要配置Apache2使用另一个端口号。
我们可以打开apache2默认端口配置文件(/etc/apache2/ports.conf)
并将侦听值更改为8080。
sudo nano /etc/apache2/ports.conf
修改如下:
# If you just change the port or add more ports here, you will likely also # have to change the VirtualHost statement in # /etc/apache2/sites-enabled/000-default.conf NameVirtualHost 127.0.0.1:8080 Listen 127.0.0.1:8080
接下来,打开Apache2默认VirtualHost配置文件。
sudo nano /etc/apache2/sites-available/000-default.conf
修改如下:
<VirtualHost 127.0.0.1:8080>
保存然后文件并退出。
然后重新启动Apache2.
sudo systemctl restart apache2.service
使用新端口访问apache2服务。
http://localhost:8080
第2步:安装Varnish
运行以下命令以安装Varnish
sudo apt-get install varnish
安装Varnish后,下面的命令可用于管理Varnish 服务
sudo systemctl stop varnish.service sudo systemctl start varnish.service sudo systemctl enable varnish.service