部署应用
使用PuTTY连接实例
- 登录ec2-user
- 为root用户添加密码,切换到root用户,修改/etc/ssh/sshd_conf文件
<code class="language-plaintext hljs">sudo passwd root</code>
<code class="language-plaintext hljs">su root</code>
<code class="language-plaintext hljs">vi /etc/ssh/sshd_config</code>
PermitRootLogin yes
PasswordAuthentication yes
<code class="language-plaintext hljs">set noreadonly :wq</code>
- 安装php、php-mysql、mariadb、httpd(LAMP)
<code class="language-plaintext hljs">sudo yum update -y</code>
<code class="language-plaintext hljs">sudo yum -y install httpd php mariadb php-mysql</code>
- 开启httpd:systemctl start httpd
<code class="language-plaintext hljs">systemctl start httpd</code>
- 设置开机自启动:systemctl enable httpd
<code class="language-plaintext hljs">systemctl status httpd</code>
<code class="language-plaintext hljs">systemctl enable httpd</code>
5、修改http配置文件,修改端口:vi /etc/httpd/conf/httpd.conf
Listen 80 修改为 Listen 8001
<code class="language-plaintext hljs">vi /etc/httpd/conf/httpd.conf</code>
<code class="language-plaintext hljs">sudo usermod -a -G apache ec2-user</code>
检查:
<code class="language-plaintext hljs">sudo id ec2-user sudo tail /etc/group</code>
<code class="language-plaintext hljs">sudo chown -R ec2-user:apache /var/www/</code>
检查:
<code class="language-plaintext hljs">sudo ls -l /var/www</code>
- 下载test项目包: wget https://tomtrain.s3.cn-northwest-1.amazonaws.com.cn/bookstore2.zip /var/www/html/
<code class="language-plaintext hljs">wget https://tomtrain.s3.cn-northwest-1.amazonaws.com.cn/bookstore2.zip /var/www/html/</code>
解压:unzip bookstore2.zip
<code class="language-plaintext hljs">unzip bookstore2.zip</code>
<code class="language-plaintext hljs">cp -r test/* /var/www/html/</code>
检查:
重启apache服务器:
<code class="language-plaintext hljs">sudo systemctl restart httpd</code>
查看侦听端口
<code class="language-plaintext hljs">sudo ss -l -t -n</code>
本地测试:
<code class="language-plaintext hljs">curl localhost:8001</code>
- 浏览器输入实例的公有IP端口为8001,测试Web服务使用正常
- 登录数据库写入节点,写入数据
<code class="language-plaintext hljs"> mysql -h rds1.cluster-cjitluvqj7lq.ap-south-1.rds.amazonaws.com -u admin -p</code>
- 创建bookstore数据库:create database bookstore;
<code class="language-plaintext hljs">create database bookstore;</code>
- 进入bookstore数据库:use bookstore;
<code class="language-plaintext hljs">use bookstore;</code>
- 创建books表:
CREATE TABLE `books` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`price` decimal(10,2) NOT NULL,
`author` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
<code class="language-plaintext hljs">CREATE TABLE `books` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `price` decimal(10,2) NOT NULL, `author` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;</code>
- 插入数据:
INSERT INTO `books` VALUES (1, 'PHP项目实践', 56.00, '明');
INSERT INTO `books` VALUES (2, 'Java基础', 88.00, '强');
INSERT INTO `books` VALUES (3, 'Python数据分析', 66.00, '飞');
<code class="language-plaintext hljs">INSERT INTO `books` VALUES (1, 'PHP项目实践', 56.00, '明'); INSERT INTO `books` VALUES (2, 'Java基础', 88.00, '强'); INSERT INTO `books` VALUES (3, 'Python数据分析', 66.00, '飞');</code>
- 检查创建是否正确:select * from books;
<code class="language-plaintext hljs">select * from books;</code>
保存:
<code class="language-plaintext hljs">commit;</code>
- 修改php文件:vi /var/www/html/index.php
<code class="language-plaintext hljs">vi /var/www/html/index.php</code>
测试:
<code class="language-plaintext hljs">curl localhost:8001</code>
- 下载压力测试文件包到/var/www/html下:
wget https://tomtrain.s3.cn-northwest-1.amazonaws.com.cn/cpuload.zip /var/www/html/
<code class="language-plaintext hljs">wget https://tomtrain.s3.cn-northwest-1.amazonaws.com.cn/cpuload.zip /var/www/html/</code>
- 解压:Unzip cpload.zip
<code class="language-plaintext hljs">unzip cpload.zip</code>
- 浏览器输入:实例IP:8001/status.php
<code class="language-plaintext hljs">实例IP:8001/status.php</code>
- 安全组名称:efs
- 描述:efs
- VPC:vpc-vpc
- 入站规则:类型为“NFS”;源为“ec2安全组”
- 其余默认
- 创建EFS
- 点击创建EFS
- 选择自定义
- 名称:EFS
- VPC:vpc-vpc
- 可用区:创建子网时选择的两个可用区
- 子网:两个Data子网
- 安全组:efs安全组
- 其它默认
- 配置EFS
(1)连接实例,安装amazon-efs-untils:yum install -y amazon-efs-utils
<code class="language-plaintext hljs">yum install -y amazon-efs-utils</code>
(2)创建EFS的挂载目录:mkdir /efs
<code class="language-plaintext hljs">mkdir /efs</code>
点击连接:
(3)挂载EFS:sudo mount -t efs -o tls fs-09b707cca4fd22e81:/ /efs
<code class="language-plaintext hljs">sudo mount -t efs -o tls fs-09b707cca4fd22e81:/ /efs</code>
注:点击创建好的EFS,点击右上角的连接可找到“使用EFS挂载帮助程序”,注意命令中挂载目录是否需要更改,这里是使用的挂载目录是“/efs”。
查看:
<code class="language-plaintext hljs">sudo df -hT</code>
<code class="language-plaintext hljs">sudo chown ec2-user:apache /efs</code>
(4)设置EFS开机自动挂载:vi /etc/fstab
注:用户指南中可找到命令,例如:fs-0b342a43a636748c1:/ /efs efs _netdev,tls 0 0
<code class="language-plaintext hljs">vi /etc/fstab</code>
<code class="language-plaintext hljs">file-system-id:/ efs-mount-point efs _netdev,tls,iam 0 0 修改为:fs-09b707cca4fd22e81:/ /efs efs _netdev,tls 0 0</code>
<code class="language-plaintext hljs">sudo umount /efs sudo mount /efs</code>
- 创建一个efs.conf: vi /etc/httpd/conf.d/efs.conf
Alias "/efs" "/efs"
<Directory "/efs">
Require all granted
</Directory>
<code class="language-plaintext hljs"> vi /etc/httpd/conf.d/efs.conf</code>
<code class="language-plaintext hljs">Alias "/efs" "/efs" <Directory "/efs"> Require all granted </Directory></code>
- 将2.jpg放到/efs目录下:cp /var/www/html/efsimags/2.jpg /efs/
<code class="language-plaintext hljs">cp /var/www/html/efsimags/2.jpg /efs/</code>