基于阿里云搭建 Web 站点(长文预警)

一:实验说明

1.1:所需资源

地域:华北6(乌兰察布)。

使用阿里云搭建一个简单的 Web 站点。

因只是实验所需,只创建 SLB、ECS、RDS 各一台,实现单机的有负载均衡的站点,不做高可用和高并发的配置;

  • 1 台 SLB,作为 Web 代理和负载均衡;

  • 1 台 ECS,作为 Web 应用服务器,部署 Wordpress;

  • 1 台 RDS,作为数据库服务器;

  • 1 个弹性公网 IP,用于后端 Web 服务器安装应用和前端负载均衡的后续访问;

  • 2 个内网子网:172.16.0.0/24 作为应用网络,连接 SLB 和 ECS;172.16.1.0/24 作为数据网络,连接 RDS;

1.2:实验过程

网络

  1. 申请一个弹性公网 IP,按流量付费;
  2. 创建 2 个子网;

ECS

  1. 创建 ECS,最低配置;
  2. 安装 Docker,拉取 wordpress 容器并启动;
  3. 绑定弹性公网 IP;
  4. 访问 Wordpress;

SLB

  1. 创建 SLB,最低配置;
  2. 配置代理后端 Web 服务器;
  3. 解绑 ECS 的公网 IP,绑定到 SLB 上;
  4. 测试通过 SLB 访问 wordpress;

RDS

  1. 创建 RDS,MySQL,最低配置;
  2. 创建 Wordpress 数据库和用户;
  3. 打通 sub1 和 sub2;
  4. 初始化 Wordpress;

验证

  1. 测试 wordpress 访问;

二:网络准备

2.1:创建弹性公网 IP

  • 申请一个弹性公网 IP,带宽 1Mbps:
地域IP地址ID实例名称状态带宽
华北6(乌兰察布 )39.101.64.176eip-0jld7o6xc08x765d67h7iFlex可用1 Mbps

2.2:创建专有网络

2.2.1:专有网络

  • 创建一个名为 “sample_net" 的专有网络,网络为 ”172.16.0.0/23“:

在这里插入图片描述

2.2.2:交换机

  • 创建两个交换机:
    sw_app(172.16.0.0/24),用于应用服务器;
    sw_data(172.16.1.0/24),用于数据库;

在这里插入图片描述

2.3:安全组

  • 创建两个个安全组:
    sg_lb:应用于 SLB,控制用户对负载均衡的访问规则;
    sg_app:应用于 ECS,用于控制负载均衡对后端 Web 服务器的访问规则(同时用于添加 RDS 白名单,以允许 ECS 实例访问 RDS 实例);
安全组Id安全组名称创建时间(UTC)VpcId
sg-0jladxnilg68dqo4x5wmsg_lb2020-11-30T07:02:35Zvpc-0jlhou6tvxoltyyoqvni
sg-0jlhnjk5tlaf96ssopkosg_app2020-11-30T06:50:50Zvpc-0jlhou6tvxoltyyoqvnix

2.3.1:sg_lb 安全组规则

  • 用于用户访问 Web 站点,对所有地址开放 80 和 443 端口:
网络类型授权方向授权策略IP协议端口范围优先级源IP地址段
intranetingressAcceptTCP22/2210.0.0.0/0
intranetingressAcceptTCP80/8010.0.0.0/0
intranetingressAcceptTCP443/44310.0.0.0/0
intranetingressAcceptICMP110.0.0.0/0

2.3.2:sg_app 安全组规则

  • 用于负载均衡访问后端 web 服务,对 172.16.0.0/24 开放 80 端口:
网络类型授权方向授权策略IP协议端口范围优先级源IP地址段
intranetingressAcceptTCP80/801172.16.0.0/24
intranetingressAcceptTCP22/2210.0.0.0/0
intranetingressAcceptICMP110.0.0.0/0

三:ECS(Web 服务)

3.1:ECS 准备

3.1.1:购买 ECS

  • 启动镜像为 CentOS 7.6:

在这里插入图片描述

3.1.2:绑定弹性公网 IP

在这里插入图片描述

3.1.3:验证 SSH 连接

在这里插入图片描述

3.2:安装配置 Nginx

  • 安装 Nginx:
[root@wordpress ~]# yum install nginx -y
  • 配置 Nginx:
[root@wordpress ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@wordpress ~]# vim /etc/nginx/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server_tokens off;
    server {
        listen       80;
        server_name  blog.furthertalk.top;
        location / {
            root   /data/nginx/wordpress;
            index index.php index.html index.htm;
        }

        location ~ \.php$ {
            root /data/nginx/wordpress;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_hide_header X-Powered-By;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ~ ^/(pm_status|ping)$ {
			include fastcgi_params;
			fastcgi_pass 127.0.0.1:9000;
			fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
		}
    }
}

[root@wordpress ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  • 启动 Nginx:
[root@wordpress ~]# systemctl start nginx
[root@wordpress ~]# systemctl enable nginx

3.3:安装配置 PHP

  • 编译安装 php-7.1.30:
[root@wordpress ~]# cd /usr/local/src/
[root@wordpress src]# tar zxvf  php-7.1.30.tar.gz
[root@wordpress src]# cd  php-7.1.30/

[root@wordpress php-7.1.30]# yum -y install wget vim pcre pcre-devel openssl openssl-devel libicudevel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpegdevel libzip

[root@wordpress php-7.1.30]# ./configure --prefix=/usr/local/php --enable-fpm --with-fpmuser=www --with-fpm-group=www --with-pear --with-curl --with-png-dir --with-freetype-dir --with-iconv --with-mhash --with-zlib --with-xmlrpc --with-xsl --with-openssl --with-mysqli --with-pdo-mysql --disable-debug --enable-zip --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-exif --enable-wddx --enable-bcmath --enable-calendar --enable-shmop --enable-dba --enable-sysvsem --enable-sysvshm --enable-sysvmsg



[root@wordpress php-7.1.30]# make -j 2
[root@wordpress php-7.1.30]# make install
  • 从相应目录拷贝 PHP 配置文件:
[root@wordpress ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@wordpress ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@wordpress ~]# cp /usr/local/src/php-7.1.30/php.ini-production /usr/local/php/etc/php.ini
  • 创建日志文件目录:
[root@wordpress ~]# mkdir /usr/local/php/log
  • 配置 pid 文件:
[root@wordpress ~]# vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
  • 将 php-fpm 添加到 service:
[root@wordpress ~]# cp /usr/local/src/php-7.1.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@wordpress ~]# chmod +x /etc/init.d/php-fpm
[root@wordpress ~]# chkconfig --add php-fpm
[root@wordpress ~]# chkconfig php-fpm on

[root@wordpress ~]# chkconfig --list                              

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 配置 www.conf:
[root@wordpress ~]# vim /usr/local/php/etc/php-fpm.d/www.conf
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 50
pm.start_servers = 30
pm.min_spare_servers = 30
pm.max_spare_servers = 50
pm.status_path = /pm_status
ping.path = /ping
ping.response = pong
access.log = log/$pool.access.log
slowlog = log/$pool.log.slow
  • 检测配置文件:
[root@wordpress ~]# /usr/local/php/sbin/php-fpm -t
[13-Nov-2020 11:07:39] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
  • 启动 php-fpm 并验证:
[root@wordpress ~]# service php-fpm start

[root@wordpress ~]# ps -ef | grep php-fpm
root      2247     1  0 11:08 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nginx     2248  2247  0 11:08 ?        00:00:00 php-fpm: pool www
nginx     2249  2247  0 11:08 ?        00:00:00 php-fpm: pool www
nginx     2250  2247  0 11:08 ?        00:00:00 php-fpm: pool www
nginx     2251  2247  0 11:08 ?        00:00:00 php-fpm: pool www
nginx     2252  2247  0 11:08 ?        00:00:00 php-fpm: pool www
nginx     2253  2247  0 11:08 ?        00:00:00 php-fpm: pool www

[root@wordpress ~]# netstat -tanlp | grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      2247/php-fpm: maste

3.4:部署 Wordpress

  • 解压 wordpress 安装包:
[root@wordpress ~]# cd /data/nginx/wordpress/
[root@wordpress wordpress]# mv index.php /tmp
[root@wordpress wordpress]# tar zxvf wordpress-5.0.3-zh_CN.tar.gz
[root@wordpress wordpress]# mv wordpress/* ./
[root@wordpress wordpress]# rmdir wordpress/
[root@wordpress wordpress]# mv wordpress-5.0.3-zh_CN.tar.gz /tmp
  • 编辑 wordpress 配置文件:
[root@wordpress wordpress]# cp wp-config-sample.php wp-config.php
[root@wordpress wordpress]# vim wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123456');
define('DB_HOST', '172.16.1.200');

四:RDS(MySQL)

4.1:购买 RDS

选择和交换机相同的可用区,这里是可用区A。

在这里插入图片描述

实例ID实例类型系列CPU(核)内存IOPS存储(GB)
rm-0jl8ic080d1ttbb5kPrimaryEnterprise81638480005
地域可用区数据库类型数据库版本网络类型
cn-wulanchabucn-wulanchabu-aMySQL8VPC

4.2:创建 Wordpress 数据库及用户

在这里插入图片描述

4.3:设置白名单

  • 允许 172.16.0.0/23 网段进行访问:

在这里插入图片描述

或者直接添加 sg_app 安全组,这样该安全组中的 ECS 实例就可以访问该 RDS 实例了。

4.4:ECS 连接 MySQL

  • 复制内网地址,使用 ECS 测试连接:
[root@wordpress ~]# mysql -hrm-0jl8ic080d1ttbb5k.mysql.rds.aliyuncs.com -uwordpress -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1924
Server version: 8.0.18-X-Cluster-1.0.5 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| wordpress          |
+--------------------+

五:SLB (负载均衡)

5.1:购买 SLB

地域:华北6(乌兰察布 )

主可用区:乌兰察布 可用区A

可用区类型:多可用区

计费项:配置费用

实例费:是

云盾:是

备可用区:乌兰察布 可用区B

实例名称:load-balancing

实例规格:简约型I (slb.s1.small)

实例类型:私网

网络类型:专有网络

专有网络:sample_net

虚拟交换机:sw_app

IP 版本:IPv4

计费方式:按使用流量计费

在这里插入图片描述

5.2:配置 SLB

采用四层 TCP 代理后端 Web 服务;

在这里插入图片描述

5.3:绑定弹性公网 IP

将 ECS 上的弹性公网 IP 解绑,绑定到 SLB 上;

六:Wordpress 访问

6.1:初始化 Wordpress

  • 填写相关管理信息,开始 Wordpress 的数据库初始化:

在这里插入图片描述

  • 进入管理页面:

在这里插入图片描述

  • 验证 Wordpress 数据库:
MySQL [(none)]> USE wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [wordpress]> SHOW TABLES;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+

6.2:绑定域名并访问

在这里插入图片描述

因域名未备案,无法通过域名访问。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值