1+ # 查询实例实例规格
2+ data "alicloud_instance_types" "default" {
3+ instance_type_family = " ecs.g7"
4+ sorted_by = " CPU"
5+ }
6+
7+ # 查询实例规格支持的可用区
8+ data "alicloud_zones" "default" {
9+ available_instance_type = data. alicloud_instance_types . default . ids . 0
10+ }
11+
12+ # 查询镜像
13+ data "alicloud_images" "default" {
14+ name_regex = " ^centos_7_8_x64*"
15+ owners = " system"
16+ instance_type = data. alicloud_instance_types . default . ids . 0
17+ }
18+
19+ resource "alicloud_vpc" "vpc" {
20+ vpc_name = " wordpress-vpc"
21+ cidr_block = " 192.168.0.0/16"
22+ }
23+
24+ resource "alicloud_vswitch" "vswitch" {
25+ vpc_id = alicloud_vpc. vpc . id
26+ cidr_block = " 192.168.0.0/24"
27+ zone_id = data. alicloud_zones . default . ids . 0
28+ vswitch_name = " wordpress-vsw"
29+ }
30+
31+ resource "alicloud_security_group" "security_group" {
32+ security_group_name = " wordpress-sg"
33+ vpc_id = alicloud_vpc. vpc . id
34+ }
35+
36+ resource "alicloud_security_group_rule" "http_ingress" {
37+ security_group_id = alicloud_security_group. security_group . id
38+ type = " ingress"
39+ ip_protocol = " tcp"
40+ port_range = " 80/80"
41+ cidr_ip = " 0.0.0.0/0"
42+ }
43+
44+ resource "alicloud_security_group_rule" "https_ingress" {
45+ security_group_id = alicloud_security_group. security_group . id
46+ type = " ingress"
47+ ip_protocol = " tcp"
48+ port_range = " 443/443"
49+ cidr_ip = " 0.0.0.0/0"
50+ }
51+
52+ resource "alicloud_instance" "ecs_instance" {
53+ instance_name = " wordpress-ecs"
54+ image_id = data. alicloud_images . default . ids . 0
55+ instance_type = data. alicloud_instance_types . default . ids . 0
56+ security_groups = [alicloud_security_group . security_group . id ]
57+ vswitch_id = alicloud_vswitch. vswitch . id
58+ internet_max_bandwidth_out = 5
59+ system_disk_category = " cloud_essd"
60+ password = var. ecs_instance_password
61+ }
62+
63+ // 在 main.tf 中添加 ECS Command 和 Invocation 资源
64+ resource "alicloud_ecs_command" "wordpress_install" {
65+ name = " wordpress-install"
66+ description = " Install WordPress on CentOS 7"
67+ type = " RunShellScript"
68+ command_content = base64encode (local. install_wordpress_script )
69+ timeout = 7200
70+ working_dir = " /root"
71+ }
72+
73+ resource "alicloud_ecs_invocation" "wordpress_install" {
74+ instance_id = [alicloud_instance . ecs_instance . id ]
75+ command_id = alicloud_ecs_command. wordpress_install . id
76+ timeouts {
77+ create = " 5m"
78+ }
79+ }
80+
81+ locals {
82+ install_wordpress_script = <<- SHELL
83+ #!/bin/bash
84+ if [ ! -f .ros.provision ]; then
85+ echo "Name: 手动搭建WordPress(CentOS 7)" > .ros.provision
86+ fi
87+
88+ name=$(grep "^Name:" .ros.provision | awk -F':' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
89+ if [[ "$name" != "手动搭建WordPress(CentOS 7)" ]]; then
90+ echo "当前实例已使用过\"$name\"教程的一键配置,不能再使用本教程的一键配置"
91+ exit 0
92+ fi
93+
94+ echo "#########################"
95+ echo "# Check Network"
96+ echo "#########################"
97+ ping -c 2 -W 2 aliyun.com > /dev/null
98+ if [[ $? -ne 0 ]]; then
99+ echo "当前实例无法访问公网"
100+ exit 0
101+ fi
102+
103+ if ! grep -q "^Step1: Prepare Environment$" .ros.provision; then
104+ echo "#########################"
105+ echo "# Prepare Environment"
106+ echo "#########################"
107+ systemctl status firewalld
108+ systemctl stop firewalld
109+ echo "Step1: Prepare Environment" >> .ros.provision
110+ else
111+ echo "#########################"
112+ echo "# Environment has been ready"
113+ echo "#########################"
114+ fi
115+
116+ if ! grep -q "^Step2: Install Nginx$" .ros.provision; then
117+ echo "#########################"
118+ echo "# Install Nginx"
119+ echo "#########################"
120+ yum -y install nginx
121+ nginx -v
122+ echo "Step2: Install Nginx" >> .ros.provision
123+ else
124+ echo "#########################"
125+ echo "# Nginx has been installed"
126+ echo "#########################"
127+ fi
128+
129+ if ! grep -q "^Step3: Install MySQL$" .ros.provision; then
130+ echo "#########################"
131+ echo "# Install MySQL"
132+ echo "#########################"
133+ rpm -Uvh https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
134+ yum -y install mysql-community-server --nogpgcheck
135+ mysql -V
136+ systemctl start mysqld
137+ systemctl enable mysqld
138+ systemctl daemon-reload
139+ echo "Step3: Install MySQL" >> .ros.provision
140+ else
141+ echo "#########################"
142+ echo "# MySQL has been installed"
143+ echo "#########################"
144+ fi
145+
146+ if ! grep -q "^Step4: Install PHP$" .ros.provision; then
147+ echo "#########################"
148+ echo "# Install PHP"
149+ echo "#########################"
150+ yum install -y \
151+ https://mirrors.aliyun.com/ius/ius-release-el7.rpm \
152+ https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
153+ rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
154+ yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
155+ php -v
156+ echo "Step4: Install PHP" >> .ros.provision
157+ else
158+ echo "#########################"
159+ echo "# PHP has been installed"
160+ echo "#########################"
161+ fi
162+
163+ if ! grep -q "^Step4: Config Nginx$" .ros.provision; then
164+ echo "#########################"
165+ echo "# Config Nginx"
166+ echo "#########################"
167+ cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
168+ cat > /etc/nginx/nginx.conf << \EOF
169+ # For more information on configuration, see:
170+ # * Official English Documentation: http://nginx.org/en/docs/
171+ # * Official Russian Documentation: http://nginx.org/ru/docs/
172+
173+ user nginx;
174+ worker_processes auto;
175+ error_log /var/log/nginx/error.log;
176+ pid /run/nginx.pid;
177+
178+ # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
179+ include /usr/share/nginx/modules/*.conf;
180+
181+ events {
182+ worker_connections 1024;
183+ }
184+
185+ http {
186+ log_format main '$remote_addr - $remote_user [$time_local] "$request" '
187+ '$status $body_bytes_sent "$http_referer" '
188+ '"$http_user_agent" "$http_x_forwarded_for"';
189+
190+ access_log /var/log/nginx/access.log main;
191+
192+ sendfile on;
193+ tcp_nopush on;
194+ tcp_nodelay on;
195+ keepalive_timeout 65;
196+ types_hash_max_size 4096;
197+
198+ include /etc/nginx/mime.types;
199+ default_type application/octet-stream;
200+
201+ include /etc/nginx/conf.d/*.conf;
202+
203+ server {
204+ listen 80;
205+ listen [::]:80;
206+ server_name _;
207+ root /usr/share/nginx/html/wordpress;
208+
209+ # Load configuration files for the default server block.
210+ include /etc/nginx/default.d/*.conf;
211+
212+ location / {
213+ index index.php index.html index.htm;
214+ }
215+
216+ location ~ .php$ {
217+ root /usr/share/nginx/html/wordpress; # 将/usr/share/nginx/html替换为您的网站根目录,本文使用/usr/share/nginx/html作为网站根目录。
218+ fastcgi_pass 127.0.0.1:9000; # Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。
219+ fastcgi_index index.php;
220+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
221+ include fastcgi_params; # Nginx调用fastcgi接口处理PHP请求。
222+ }
223+
224+ error_page 404 /404.html;
225+ location = /404.html {
226+ }
227+
228+ error_page 500 502 503 504 /50x.html;
229+ location = /50x.html {
230+ }
231+ }
232+
233+ }
234+ EOF
235+ systemctl start nginx
236+ systemctl enable nginx
237+ echo "Step4: Config Nginx" >> .ros.provision
238+ else
239+ echo "#########################"
240+ echo "# Nginx has been configured"
241+ echo "#########################"
242+ fi
243+
244+ if ! grep -q "^Step6: Config MySQL$" .ros.provision; then
245+ echo "#########################"
246+ echo "# Config MySQL"
247+ echo "#########################"
248+ export MYSQL_PWD=`grep "temporary password" /var/log/mysqld.log | awk '{print $NF}'`
249+ mysqladmin -uroot password '${ var . db_password } '
250+ export MYSQL_PWD='${ var . db_password } '
251+ mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${ var . db_password } '"
252+ echo CREATE DATABASE wordpress\; >> /tmp/setup.mysql
253+ echo CREATE user "user"@"localhost" identified by '"${ var . db_password } "'\; >> /tmp/setup.mysql
254+ echo GRANT ALL privileges ON wordpress.* TO "user"@"localhost" IDENTIFIED BY '"${ var . db_password } "'\; >> /tmp/setup.mysql
255+ echo FLUSH privileges\;>> /tmp/setup.mysql
256+ chmod 400 /tmp/setup.mysql
257+ mysql -u root --password='${ var . db_password } ' < /tmp/setup.mysql
258+ echo "Step6: Config MySQL" >> .ros.provision
259+ else
260+ echo "#########################"
261+ echo "# MySQL has been configured"
262+ echo "#########################"
263+ fi
264+
265+ if ! grep -q "^Step7: Config PHP$" .ros.provision; then
266+ echo "#########################"
267+ echo "# Config PHP"
268+ echo "#########################"
269+ echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/phpinfo.php
270+ systemctl start php-fpm
271+ systemctl enable php-fpm
272+ echo "Step7: Config PHP" >> .ros.provision
273+ else
274+ echo "#########################"
275+ echo "# PHP has been configured"
276+ echo "#########################"
277+ fi
278+
279+ if ! grep -q "^Step8: Install wordpress$" .ros.provision; then
280+ echo "#########################"
281+ echo "# Install wordpress"
282+ echo "#########################"
283+ yum -y install wordpress
284+ echo "Step8: Install wordpress" >> .ros.provision
285+ else
286+ echo "#########################"
287+ echo "# wordpress has been installed"
288+ echo "#########################"
289+ fi
290+
291+ if ! grep -q "^Step9: Config wordpress$" .ros.provision; then
292+ echo "#########################"
293+ echo "# Config wordpress"
294+ echo "#########################"
295+ mv /usr/share/wordpress /usr/share/nginx/html/wordpress
296+ cd /usr/share/nginx/html/wordpress
297+ ln -snf /etc/wordpress/wp-config.php wp-config.php
298+ sed -i "s/database_name_here/wordpress/" wp-config.php
299+ sed -i "s/username_here/user/" wp-config.php
300+ sed -i "s/password_here/${ var . db_password } /" wp-config.php
301+ echo "Step8: Config wordpress" >> .ros.provision
302+ else
303+ echo "#########################"
304+ echo "# wordpress has been configured"
305+ echo "#########################"
306+ fi
307+
308+ systemctl restart nginx
309+ SHELL
310+ }
0 commit comments