为生产而构建:网络应用程序 - 部署

简介

在本教程的这一部分中,我们将部署我们的示例PHP应用程序WordPress和一个私有DNS:

dns+应用程序Diagram

您的用户将通过指向负载均衡器的域名(例如https://www.example.com)通过HTTPS访问您的应用程序。负载均衡器将充当应用程序服务器的反向代理,应用程序服务器将连接到数据库服务器。私有DNS将使我们能够使用名称来引用我们服务器的私有网络地址,从而简化我们服务器的配置过程。

我们将按照以下顺序在六台服务器上设置我们刚才讨论的组件:

  • 内网域名(NS1和NS2)
  • 数据库服务器(DB1)
  • 应用服务器(App1和App2)
  • 负载均衡(Lb1)

让我们开始进行DNS设置。

内网域名服务器

使用地址名称有助于识别您正在使用的服务器,并且对于维护更大的服务器设置是必不可少的,因为您可以通过简单地(在单个位置)更新您的DNS记录来替换服务器,而不是使用IP地址更新无数的配置文件。在我们的设置中,我们将设置我们的DNS,这样我们就可以通过名称而不是IP地址来引用服务器的专用网络地址。

我们将通过nyc3.example.com子域下的主机名来引用每个服务器的专用网络地址。例如,数据库服务器的专用网络地址将是db1.nyc3.example.com,它将解析为其专用IP地址。请注意,示例子域几乎完全是任意的,通常是根据逻辑组织目的来选择的;在我们的示例中,NYC3表示服务器位于NYC3数据中心,而Example.com是我们的应用程序的域名。

按照本教程进行设置,并为您的设置中的每个服务器添加DNS记录:

完成DNS教程后,您应该有两个BIND服务器:ns 1 和 ** ns 2** 。如果您已经知道设置中所有服务器的私有IP地址,请立即将它们添加到DNS中;否则,请在创建服务器时添加相应的DNS记录。

现在我们准备好设置数据库服务器了。

设置数据库服务器

因为我们想要对我们的应用程序服务器(即运行Apache和PHP的应用程序服务器)进行负载平衡,所以我们需要通过在单独的服务器上设置数据库来将数据库与应用程序服务器分离。在水平扩展许多类型的应用程序之前,将数据库与应用程序分离是必不可少的一步,正如这篇博客文章所解释的那样:水平扩展PHP应用程序:实用的Overview.

本节介绍了设置数据库服务器的所有必要步骤,但您可以在本教程中了解到更多关于为php应用程序设置远程、分离的mySQL数据库服务器的信息:如何设置远程MySQLMYSQL Database.

安装MySql

在数据库服务器DB1 上,安装MySQL Server:

1sudo apt-get update
2sudo apt-get -y install mysql-server

在提示符下输入所需的MySQL root密码。

现在运行:

1sudo mysql_install_db
2sudo mysql_secure_installation

您必须输入在上述步骤中设置的MySQL管理员密码。之后,它会询问您是否要更改该密码。如果您对当前密码满意,请输入N表示否。用默认设置回答其余问题。

配置MySQL监听内网接口

打开MySQL配置文件:

1sudo vi /etc/mysql/my.cnf

找到绑定地址设置,修改为您的数据库服务器的内网地址:

1[label /etc/mysql/my.cnf]
2bind-address            = db1.nyc3.example.com

保存并退出。

重启MySQL:

1sudo service mysql restart

设置数据库和数据库用户

现在,我们需要创建应用服务器用来连接的数据库和数据库用户。

进入MySQL控制台:

1mysql -u root -p

在提示符下输入MySQL超级用户密码。

在MySQL提示符下,为您的应用程序创建数据库:

1CREATE DATABASE app;

MySQL将其用户关联到他们应该连接的服务器。在我们的例子中,我们有两个将要连接的应用程序服务器,所以我们应该为每个服务器创建一个用户。创建一个数据库用户,在我们的示例中是)的私有网络地址连接到。您应该为每个用户使用相同的密码:

1CREATE USER 'appuser'@'app1.nyc3.example.com' IDENTIFIED BY 'password';
2CREATE USER 'appuser'@'app2.nyc3.example.com' IDENTIFIED BY 'password';

我们稍后将配置最终的数据库用户权限,但让我们给appuser 对** app** 数据库的完全控制权:

1GRANT ALL PRIVILEGES ON app.* TO 'appuser'@'app1.nyc3.example.com';
2GRANT ALL PRIVILEGES ON app.* TO 'appuser'@'app2.nyc3.example.com';
3FLUSH PRIVILEGES;

这些宽松的特权确保应用程序的安装程序能够在数据库中安装应用程序。如果您有两个以上的应用程序服务器,那么现在就应该创建所有必要的数据库用户。

现在退出MySQL提示符:

1exit

数据库服务器设置完成。让我们设置应用程序服务器。

设置应用服务器

应用程序服务器将运行我们的应用程序的代码,该代码将连接到数据库服务器。我们的示例应用程序是WordPress,这是一个PHP应用程序,它通过Web服务器(如Apache或Nginx)提供服务。因为我们想要对应用程序服务器进行负载平衡,所以我们将设置两个相同的服务器。

本部分介绍了设置应用程序服务器的所有必要步骤,但该主题将在以下教程中详细介绍,从设置Web服务器 部分开始:如何设置远程Database.

安装APACHE和PHP

app1 和** app2** 这两个应用服务器上,安装Apache和PHP:

1sudo apt-get update
2sudo apt-get -y install apache2 php5-mysql php5 libapache2-mod-php5 php5-mcrypt

配置Apache

我们将在负载均衡器服务器上使用HAProxy来处理SSL终止,因此我们不希望我们的用户直接访问应用程序服务器。因此,我们将把Apache绑定到每个服务器的专用网络地址。

在每个应用服务器上,app1 和** app2** ,打开您的apache端口配置文件。默认情况下,这是ports.con文件:

1sudo vi /etc/apache2/ports.conf

找到Listen 80的行,并在其中添加您的私有IP地址,如下所示(替换为您服务器的实际IP地址):

1[label Apache ports.conf — Listen on private interface]
2Listen private_IP:80

保存并退出。这会将Apache配置为仅侦听专用网络接口,这意味着无法通过公共IP地址或主机名访问该接口。

重新启动Apache以使更改生效:

1sudo service apache2 restart

现在,只能通过您的应用程序服务器的专用网络地址访问Apache.我们将在这里配置负载均衡器以发送用户请求,稍后。

下载并配置应用

在我们的例子中,我们使用WordPress作为我们的应用程序。如果您使用的是不同的PHP应用程序,请下载它并执行任何相关配置(例如数据库连接信息),然后跳到下一节。

在第一个应用服务器app1 上,下载WordPress归档文件:

1cd ~
2wget http://wordpress.org/latest.tar.gz

解压WordPress存档文件:

1tar xvf latest.tar.gz

切换到解压缩目录:

1cd wordpress

WordPress需要为其上传创建一个目录,即wp-content/ploads。让我们现在就开始吧:

1mkdir wp-content/uploads

我们将使用示例WordPress配置文件作为模板。将其复制到正确的位置:

1cp wp-config-sample.php wp-config.php

现在打开配置文件进行编辑:

1vi wp-config.php

通过更改以下行中突出显示的信息来配置WordPress数据库连接:

 1[label wp-config.php]
 2/** The name of the database for WordPress */
 3define('DB_NAME', 'app');
 4
 5/** MySQL database username */
 6define('DB_USER', 'appuser');
 7
 8/** MySQL database password */
 9define('DB_PASSWORD', 'password');
10
11/** MySQL hostname */
12define('DB_HOST', 'db1.nyc3.example..com');

因为我们将在负载均衡器服务器上使用TLS/SSL加密,所以我们必须添加以下行,以便WordPress知道它位于使用SSL的反向代理之后:

1define('FORCE_SSL_ADMIN', true);
2if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
3       $_SERVER['HTTPS']='on';

您还需要更新密钥和盐,以便可以在需要时使Cookie无效。我们不会在这里讨论这一点,但请确保它们在所有应用程序服务器上都是相同的。

保存并退出。

WordPress现在已经配置好了,但它的文件必须被复制到正确的位置才能由我们的网络服务器软件提供服务。

将应用程序文件复制到文档根目录

现在我们已经配置了应用程序,我们需要将其复制到Apache的文档根目录中,在那里它可以提供给我们网站的访问者。

ApacheDocumentRoot的默认位置是/var/www/html,因此我们将在我们的示例中使用该位置。

首先,删除默认的index.html文件:

1sudo rm /var/www/html/index.html

然后使用rsync将WordPress文件复制到/var/www/html,并将www-data(运行apache的用户)作为所有者:

1sudo rsync -avP ~/wordpress/ /var/www/html
2sudo chgrp -R www-data /var/www/html/*

我们的第一个应用服务器app1已经准备好了。我们将设置另一个应用程序服务器。

将应用程序文件复制到其他服务器

为了在各种应用程序服务器上保持应用程序文件的一致性,您应该设置Web服务器的文档根目录的文件复制。在WordPress的情况下,使用Web界面上传文件和安装插件会将文件存储在处理请求的特定服务器上。如果没有将这些文件复制到您的所有应用程序服务器,则您的一些用户将收到丢失图像和损坏插件的页面。如果您的PHP应用程序不是WordPress,并且没有在应用程序服务器上存储任何数据(例如,上传的文件或下载的插件),您只需手动复制应用程序文件一次。如果是这种情况,请使用rsync将您的应用程序文件从app1 复制到** app2** 。

GlusterFS可用于创建必要文件的复制卷,并在本教程的同步Web应用程序文件 部分中进行了演示:如何使用HAProxy作为WordPress应用程序Servers.的负载均衡器按照说明操作(跳过_编辑主机文件_一节,因为我们的DNS会负责这一点),并在** app1** 和** app2** 之间设置复制。

正确设置复制后,两个应用程序服务器都应正确配置。现在让我们设置负载均衡器。

设置负载均衡服务器

我们的负载平衡器服务器将运行HAProxy,它将充当我们的应用程序服务器的反向代理负载平衡器。您的用户将通过诸如https://www.example.com.之类的URL通过负载均衡服务器访问您的应用程序

本节涵盖了设置负载均衡器服务器的所有必要步骤,但在以下教程中详细介绍了该主题:

复制SSL证书

在负载平衡器服务器 lb1 上执行这些步骤。

在包含您的SSL证书(第1部分的先决条件之一)的目录中,将您的证书、任何中间CA证书和您的证书的密钥组合到一个.pem文件中。例如(我们的证书在/根/certs中:

1cd /root/certs
2cat www.example.com.crt CAintermediate.ca-bundle www.example.com.key > www.example.com.pem

然后将pem文件复制到/etc/ssl/Priate

1sudo cp www.example.com.pem /etc/ssl/private/

This file will be used by HAProxy for SSL termination.

Install HAProxy

On the load balancer server, lb1 , install HAProxy:

1sudo add-apt-repository ppa:vbernat/haproxy-1.5
2sudo apt-get update
3sudo apt-get -y install haproxy

Now let's configure HAProxy.

HAProxy Configuration

We need to configure HAProxy with some reasonable settings, SSL termination, and the appropriate frontends and backends to make it work with our application servers.

Open the HAProxy configuration file for editing:

1sudo vi /etc/haproxy/haproxy.cfg

HAProxy Configuration: General Settings

The first thing you will want to do is set maxconn to a reasonable number. This setting affects how many concurrent connections HAProxy will allow, which can affect QoS and prevent your web servers from crashing from trying to serve too many requests. You will need to play around with it to find what works for your environment. Add the following line (with a value you think is reasonable) to the global section of the configuration:

1[label haproxy.cfg — maxconn]
2   maxconn 2048

Add this line, to configure the maximum size of temporary DHE keys that are generated:

1[label haproxy.cfg — tune.ssl.default-dh-param]
2   tune.ssl.default-dh-param 2048

Next, in the defaults section, add the following lines under the line that says mode http:

1[label haproxy.cfg ]
2   option forwardfor
3   option http-server-close

If you would like to enable the HAProxy stats page, add the following lines in the defaults section (substitute user and password with secure values):

1[label haproxy.cfg ]
2   stats enable
3   stats uri /stats
4   stats realm Haproxy\ Statistics
5   stats auth user:password

This will allow you to look at the HAProxy stats page by going to your domain on /stats (e.g. "https://www.example.com/stats").

Do not close the config file yet! We will add the proxy configuration next.

HAProxy Configuration: Proxies

The first thing we want to add is a frontend to handle incoming HTTP connections. At the end of the file, let's add a frontend called www-http:

1frontend www-http
2   bind www.example.com:80
3   reqadd X-Forwarded-Proto:\ http
4   default_backend app-backend

The purpose of this frontend is to accept HTTP connections so they can be redirected to HTTPS.

Now add a frontend to handle the incoming HTTPS connections. Make sure to specify the appropriate pem certificate:

1frontend www-https
2   bind www.example.com:443 ssl crt /etc/ssl/private/www.example.com.pem
3   reqadd X-Forwarded-Proto:\ https
4   default_backend app-backend

After you are finished configuring the frontends, continue adding your backend by adding the following lines:

1backend app-backend
2   redirect scheme https if !{ ssl_fc }
3   server app1 app1.nyc3.example.com:80 check
4   server app2 app2.nyc3.example.com:80 check

This backend specifies which application servers to send the load balanced traffic to. Also, the redirect scheme https line tells it to redirect HTTP connections to HTTPS.

Now save and exit haproxy.cfg. HAProxy is now ready to be started, but let's enable logging first.

Enable HAProxy Logging

Open the rsyslog configuration file:

1sudo vi /etc/rsyslog.conf

Then find the following lines and uncomment them to enable UDP syslog reception. It should look like the following when you're done:

1[label /etc/rsyslog.conf]
2$ModLoad imudp
3$UDPServerRun 514
4$UDPServerAddress 127.0.0.1

Now restart rsyslog to enable the new configuration:

1sudo service rsyslog restart

HAProxy logging is is now enabled! The log file will be created at /var/log/haproxy.log once HAProxy is started.

Restart HAProxy

Restart HAProxy to put the changes into effect:

1sudo service haproxy restart

Our load balancer is now set up.

Now we need to run the application's install script.

Install WordPress

We must run the WordPress installation script, which prepares the database for its use, before we can use it.

Open your site in a web browser:

1[secondary_label Open in a Web Browser]
2https://www.example.com/wp-admin/install.php

This will display the WordPress installation screen. Fill out the forms and click the Install WordPress button.

After WordPress installs, the application is ready to be used.

Conclusion

The servers that comprise your application are now set up, and your application is ready to be used. You may log in as the admin user, and your users may access the site over HTTPS via the proper domain name.

Be sure to test out your application and make sure that it works as expected before moving on.

Continue to the next tutorial to start working on the recovery plan for your production application setup: Building for Production: Web Applications — Recovery Planning.

Published At
Categories with 技术
comments powered by Disqus