1
+ provider "alicloud" {
2
+ region = var. region_id
3
+ }
4
+ resource "random_id" "suffix" {
5
+ byte_length = 8
6
+ }
7
+
8
+ locals {
9
+ common_name = random_id. suffix . id
10
+ ecs_command = << SHELL
11
+ #!/bin/bash
12
+ cat << INNER_EOF >> ~/.bash_profile
13
+ export DB_NAME=${ var . db_name }
14
+ export DB_USERNAME=${ var . db_user }
15
+ export DB_PASSWORD=${ var . db_password }
16
+ export DB_CONNECTION=${ alicloud_db_instance . rds_db_instance . connection_string }
17
+ export ROS_DEPLOY=true
18
+ INNER_EOF
19
+
20
+ source ~/.bash_profile
21
+
22
+ curl -fsSL https://static-aliyun-doc.oss-cn-hangzhou.aliyuncs.com/install-script/develop-your-wechat-mini-program-in-10-minutes/install.sh|bash
23
+
24
+ ## 调整db连接配置
25
+ sed -i 's/localhost/${ alicloud_db_instance . rds_db_instance . connection_string } /' /var/www/html/wp-config.php
26
+ sed -i 's/username_here/${ var . db_user } /' /var/www/html/wp-config.php
27
+ sed -i 's/password_here/${ var . db_password } /' /var/www/html/wp-config.php
28
+ sed -i 's/database_name_here/${ var . db_name } /' /var/www/html/wp-config.php
29
+
30
+ cd /var/www/html
31
+ sudo cat << INNER_EOF > .htaccess
32
+ # BEGIN WordPress
33
+ <IfModule mod_rewrite.c>
34
+ RewriteEngine On
35
+ RewriteCond \%\{HTTP:Authorization\} ^(.*)
36
+ RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
37
+ RewriteBase /
38
+ RewriteRule ^index\.php$ - [L]
39
+ RewriteCond \%\{REQUEST_FILENAME\} !-f
40
+ RewriteCond \%\{REQUEST_FILENAME\} !-d
41
+ RewriteRule . /index.php [L]
42
+ </IfModule>
43
+ # END WordPress
44
+ INNER_EOF
45
+ sed -i 's/AllowOverride None/AllowOverride All/g' /etc/httpd/conf/httpd.conf
46
+
47
+ wget https://downloads.wordpress.org/plugin/jwt-authentication-for-wp-rest-api.zip
48
+ yum -y install unzip
49
+ unzip jwt-authentication-for-wp-rest-api.zip -d jwt-authentication-for-wp-rest-api
50
+ cp -r ./jwt-authentication-for-wp-rest-api/jwt-authentication-for-wp-rest-api /var/www/html/wp-content/plugins
51
+ rm -rf jwt-authentication-for-wp-rest-api.zip
52
+ rm -rf jwt-authentication-for-wp-rest-api
53
+ wget https://gitee.com/qin-yangming/open-tools/raw/master/wp-cli.phar
54
+ chmod +x wp-cli.phar
55
+ mv wp-cli.phar /usr/local/bin/wp
56
+
57
+ SECRET_KEY=$(openssl rand -base64 32) && sed -i "/Database settings/i define('JWT_AUTH_SECRET_KEY', '$SECRET_KEY');\ndefine('JWT_AUTH_CORS_ENABLE', true);\n" /var/www/html/wp-config.php
58
+ sed -i 's/\r$//' /var/www/html/wp-config.php
59
+ wp core install --url=${ alicloud_instance . ecs_instance . public_ip } --title="Hello World" --admin_user=${ var . word_press_user_name } --admin_password=${ var . word_press_password } --admin_email=${ var . word_press_user_email } --skip-email --allow-root
60
+
61
+ wp plugin activate jwt-authentication-for-wp-rest-api --allow-root --path=/var/www/html
62
+
63
+ systemctl restart httpd
64
+ SHELL
65
+ }
66
+
67
+ data "alicloud_instance_types" "default" {
68
+ cpu_core_count = 2
69
+ memory_size = 2
70
+ }
71
+
72
+ data "alicloud_images" "default" {
73
+ name_regex = " ^centos_7_9_x64_20G_alibase_*"
74
+ most_recent = true
75
+ owners = " system"
76
+ }
77
+
78
+ data "alicloud_zones" "default" {
79
+ available_resource_creation = " Instance"
80
+ available_instance_type = data. alicloud_instance_types . default . instance_types [0 ]. id
81
+ }
82
+
83
+ data "alicloud_db_instance_classes" "example" {
84
+ zone_id = data. alicloud_zones . default . zones [0 ]. id
85
+ engine = " MySQL"
86
+ engine_version = " 8.0"
87
+ category = " Basic"
88
+ db_instance_storage_type = " cloud_essd"
89
+ instance_charge_type = " PostPaid"
90
+ }
91
+
92
+ resource "alicloud_vpc" "vpc" {
93
+ vpc_name = " vpc"
94
+ cidr_block = var. vpc_cidr_block
95
+ }
96
+
97
+ resource "alicloud_vswitch" "vswitch" {
98
+ vpc_id = alicloud_vpc. vpc . id
99
+ cidr_block = var. vswitch_cidr_block
100
+ zone_id = data. alicloud_zones . default . zones [0 ]. id
101
+ vswitch_name = " vsw"
102
+ }
103
+
104
+ # Security Group
105
+ resource "alicloud_security_group" "security_group" {
106
+ vpc_id = alicloud_vpc. vpc . id
107
+ security_group_name = " sg"
108
+ security_group_type = " normal"
109
+ }
110
+
111
+ resource "alicloud_security_group_rule" "http" {
112
+ type = " ingress"
113
+ ip_protocol = " tcp"
114
+ port_range = " 80/80"
115
+ cidr_ip = " 0.0.0.0/0"
116
+ security_group_id = alicloud_security_group. security_group . id
117
+ }
118
+
119
+ resource "alicloud_security_group_rule" "https" {
120
+ type = " ingress"
121
+ ip_protocol = " tcp"
122
+ port_range = " 22/22"
123
+ cidr_ip = " 0.0.0.0/0"
124
+ security_group_id = alicloud_security_group. security_group . id
125
+ }
126
+
127
+ # RDS Resources
128
+ resource "alicloud_db_instance" "rds_db_instance" {
129
+ engine = " MySQL"
130
+ engine_version = " 8.0"
131
+ instance_type = data. alicloud_db_instance_classes . example . instance_classes . 0 . instance_class
132
+ instance_storage = data. alicloud_db_instance_classes . example . instance_classes . 0 . storage_range . min
133
+ db_instance_storage_type = " cloud_essd"
134
+ vswitch_id = alicloud_vswitch. vswitch . id
135
+ zone_id = data. alicloud_zones . default . zones [0 ]. id
136
+ security_group_ids = [alicloud_security_group . security_group . id ]
137
+ }
138
+
139
+ resource "alicloud_db_database" "rds_database" {
140
+ instance_id = alicloud_db_instance. rds_db_instance . id
141
+ name = var. db_name
142
+ character_set = " utf8mb4"
143
+ }
144
+
145
+ resource "alicloud_db_account" "rds_account" {
146
+ db_instance_id = alicloud_db_instance. rds_db_instance . id
147
+ account_name = var. db_user
148
+ account_type = " Normal"
149
+ account_password = var. db_password
150
+ }
151
+
152
+ resource "alicloud_db_account_privilege" "rds_account_privilege" {
153
+ instance_id = alicloud_db_instance. rds_db_instance . id
154
+ account_name = alicloud_db_account. rds_account . account_name
155
+ db_names = alicloud_db_database. rds_database . * . name
156
+ privilege = " ReadWrite"
157
+ }
158
+
159
+ # ECS Resources
160
+ resource "alicloud_instance" "ecs_instance" {
161
+ instance_name = " ecs-${ local . common_name } "
162
+ system_disk_category = " cloud_essd"
163
+ image_id = " centos_7_9_x64_20G_alibase_20240628.vhd"
164
+ vswitch_id = alicloud_vswitch. vswitch . id
165
+ password = var. ecs_instance_password
166
+ instance_type = data. alicloud_instance_types . default . instance_types [0 ]. id
167
+ internet_max_bandwidth_out = 5
168
+ security_groups = [alicloud_security_group . security_group . id ]
169
+ }
170
+ resource "alicloud_ecs_command" "run_command" {
171
+ name = " commond_install"
172
+ description = " commond_install_description"
173
+ enable_parameter = false
174
+ type = " RunShellScript"
175
+ command_content = base64encode (local. ecs_command )
176
+ timeout = 3600
177
+ working_dir = " /root"
178
+ }
179
+ resource "alicloud_ecs_invocation" "run_command" {
180
+ instance_id = [alicloud_instance . ecs_instance . id ]
181
+ command_id = alicloud_ecs_command. run_command . id
182
+ timeouts {
183
+ create = " 10m"
184
+ }
185
+ }
0 commit comments