Skip to content

Commit bb68194

Browse files
authored
Merge pull request #103544 from vjirovsky-msft/feature/change-aks-wp-to-nginx
improved Wordpress on AKS tutorial
2 parents effedb3 + e7c1bb1 commit bb68194

File tree

1 file changed

+106
-95
lines changed

1 file changed

+106
-95
lines changed

articles/mysql/flexible-server/tutorial-deploy-wordpress-on-aks.md

Lines changed: 106 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -114,86 +114,11 @@ The server created has the below attributes:
114114
- Using public-access argument allow you to create a server with public access protected by firewall rules. By providing your IP address to add the firewall rule to allow access from your client machine.
115115
- Since the command is using Local context it will create the server in the resource group ```wordpress-project``` and in the region ```eastus```.
116116

117-
### Build your WordPress docker image
118-
119-
Download the [latest WordPress](https://wordpress.org/download/) version. Create new directory ```my-wordpress-app``` for your project and use this simple folder structure
120-
121-
```wordpress
122-
└───my-wordpress-app
123-
└───public
124-
├───wp-admin
125-
│ ├───css
126-
. . . . . . .
127-
├───wp-content
128-
│ ├───plugins
129-
. . . . . . .
130-
└───wp-includes
131-
. . . . . . .
132-
├───wp-config-sample.php
133-
├───index.php
134-
. . . . . . .
135-
└─── Dockerfile
117+
## Container definitions
136118

137-
```
138-
139-
Rename ```wp-config-sample.php``` to ```wp-config.php``` and replace lines from beginingin of ```// ** MySQL settings - You can get this info from your web host ** //``` until the line ```define( 'DB_COLLATE', '' );``` with the code snippet below. The code below is reading the database host, username and password from the Kubernetes manifest file.
140-
141-
```php
142-
//Using environment variables for DB connection information
143-
144-
// ** MySQL settings - You can get this info from your web host ** //
145-
/** The name of the database for WordPress */
146-
147-
$connectstr_dbhost = getenv('DATABASE_HOST');
148-
$connectstr_dbusername = getenv('DATABASE_USERNAME');
149-
$connectstr_dbpassword = getenv('DATABASE_PASSWORD');
150-
$connectst_dbname = getenv('DATABASE_NAME');
151-
152-
/** MySQL database name */
153-
define('DB_NAME', $connectst_dbname);
154-
155-
/** MySQL database username */
156-
define('DB_USER', $connectstr_dbusername);
157-
158-
/** MySQL database password */
159-
define('DB_PASSWORD',$connectstr_dbpassword);
160-
161-
/** MySQL hostname */
162-
define('DB_HOST', $connectstr_dbhost);
119+
In the following example, we're creating two containers, a Nginx web server and a PHP FastCGI processor, based on official Docker images `nginx` and `wordpress` ( `fpm` version with FastCGI support), published on Docker Hub.
163120

164-
/** Database Charset to use in creating database tables. */
165-
define('DB_CHARSET', 'utf8');
166-
167-
/** The Database Collate type. Don't change this if in doubt. */
168-
define('DB_COLLATE', '');
169-
170-
171-
/** SSL*/
172-
define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL);
173-
```
174-
175-
### Create a Dockerfile
176-
177-
Create a new Dockerfile and copy this code snippet. This Dockerfile in setting up Apache web server with PHP and enabling mysqli extension.
178-
179-
```docker
180-
FROM php:7.2-apache
181-
COPY public/ /var/www/html/
182-
RUN docker-php-ext-install mysqli
183-
RUN docker-php-ext-enable mysqli
184-
```
185-
186-
### Build your docker image
187-
188-
Make sure you're in the directory ```my-wordpress-app``` in a terminal using the ```cd``` command. Run the following command to build the image:
189-
190-
``` bash
191-
192-
docker build --tag myblog:latest .
193-
194-
```
195-
196-
Deploy your image to [Docker hub](https://docs.docker.com/get-started/part3/#create-a-docker-hub-repository-and-push-your-image) or [Azure Container registry](../../container-registry/container-registry-get-started-azure-cli.md).
121+
Alternatively you can build custom docker image(s) and deploy image(s) into [Docker hub](https://docs.docker.com/get-started/part3/#create-a-docker-hub-repository-and-push-your-image) or [Azure Container registry](../../container-registry/container-registry-get-started-azure-cli.md).
197122

198123
> [!IMPORTANT]
199124
> If you are using Azure container regdistry (ACR), then run the ```az aks update``` command to attach ACR account with the AKS cluster.
@@ -202,44 +127,71 @@ Deploy your image to [Docker hub](https://docs.docker.com/get-started/part3/#cre
202127
> az aks update -n myAKSCluster -g wordpress-project --attach-acr <your-acr-name>
203128
> ```
204129
130+
205131
## Create Kubernetes manifest file
206132
207133
A Kubernetes manifest file defines a desired state for the cluster, such as what container images to run. Let's create a manifest file named `mywordpress.yaml` and copy in the following YAML definition.
208134
209135
> [!IMPORTANT]
210136
>
211-
> - Replace ```[DOCKER-HUB-USER/ACR ACCOUNT]/[YOUR-IMAGE-NAME]:[TAG]``` with your actual WordPress docker image name and tag, for example ```docker-hub-user/myblog:latest```.
212137
> - Update ```env``` section below with your ```SERVERNAME```, ```YOUR-DATABASE-USERNAME```, ```YOUR-DATABASE-PASSWORD``` of your MySQL flexible server.
213138
214139
```yaml
215140
apiVersion: apps/v1
216141
kind: Deployment
217142
metadata:
218-
name: wordpress-blog
143+
name: wp-blog
219144
spec:
220145
replicas: 1
221146
selector:
222147
matchLabels:
223-
app: wordpress-blog
148+
app: wp-blog
224149
template:
225150
metadata:
226151
labels:
227-
app: wordpress-blog
152+
app: wp-blog
228153
spec:
229154
containers:
230-
- name: wordpress-blog
231-
image: [DOCKER-HUB-USER-OR-ACR-ACCOUNT]/[YOUR-IMAGE-NAME]:[TAG]
155+
- name: wp-blog-nginx
156+
image: nginx
232157
ports:
233158
- containerPort: 80
159+
volumeMounts:
160+
- name: config
161+
mountPath: /etc/nginx/conf.d
162+
- name: wp-persistent-storage
163+
mountPath: /var/www/html
164+
165+
- name: wp-blog-php
166+
image: wordpress:fpm
167+
ports:
168+
- containerPort: 9000
169+
volumeMounts:
170+
- name: wp-persistent-storage
171+
mountPath: /var/www/html
234172
env:
235-
- name: DATABASE_HOST
236-
value: "SERVERNAME.mysql.database.azure.com" #Update here
237-
- name: DATABASE_USERNAME
238-
value: "YOUR-DATABASE-USERNAME" #Update here
239-
- name: DATABASE_PASSWORD
240-
value: "YOUR-DATABASE-PASSWORD" #Update here
241-
- name: DATABASE_NAME
242-
value: "flexibleserverdb"
173+
- name: WORDPRESS_DB_HOST
174+
value: "<<SERVERNAME.mysql.database.azure.com>>" #Update here
175+
- name: WORDPRESS_DB_USER
176+
value: "<<YOUR-DATABASE-USERNAME>>" #Update here
177+
- name: WORDPRESS_DB_PASSWORD
178+
value: "<<YOUR-DATABASE-PASSWORD>>" #Update here
179+
- name: WORDPRESS_DB_NAME
180+
value: "<<flexibleserverdb>>"
181+
- name: WORDPRESS_CONFIG_EXTRA # enable SSL connection for MySQL
182+
value: |
183+
define('MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL);
184+
volumes:
185+
- name: config
186+
configMap:
187+
name: wp-nginx-config
188+
items:
189+
- key: config
190+
path: site.conf
191+
192+
- name: wp-persistent-storage
193+
persistentVolumeClaim:
194+
claimName: wp-pv-claim
243195
affinity:
244196
podAntiAffinity:
245197
requiredDuringSchedulingIgnoredDuringExecution:
@@ -248,19 +200,78 @@ spec:
248200
- key: "app"
249201
operator: In
250202
values:
251-
- wordpress-blog
203+
- wp-blog
252204
topologyKey: "kubernetes.io/hostname"
253205
---
254206
apiVersion: v1
207+
kind: PersistentVolumeClaim
208+
metadata:
209+
name: wp-pv-claim
210+
labels:
211+
app: wp-blog
212+
spec:
213+
accessModes:
214+
- ReadWriteOnce
215+
resources:
216+
requests:
217+
storage: 20Gi
218+
---
219+
apiVersion: v1
255220
kind: Service
256221
metadata:
257-
name: php-svc
222+
name: blog-nginx-service
258223
spec:
259224
type: LoadBalancer
260225
ports:
261226
- port: 80
262227
selector:
263-
app: wordpress-blog
228+
app: wp-blog
229+
---
230+
apiVersion: v1
231+
kind: ConfigMap
232+
metadata:
233+
name: wp-nginx-config
234+
data:
235+
config : |
236+
server {
237+
listen 80;
238+
server_name localhost;
239+
root /var/www/html/;
240+
241+
access_log /var/log/nginx/wp-blog-access.log;
242+
error_log /var/log/nginx/wp-blog-error.log error;
243+
index index.html index.htm index.php;
244+
245+
246+
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
247+
expires max;
248+
index index.php index.html index.htm;
249+
try_files $uri =404;
250+
}
251+
252+
location / {
253+
index index.php index.html index.htm;
254+
255+
if (-f $request_filename) {
256+
expires max;
257+
break;
258+
}
259+
260+
if (!-e $request_filename) {
261+
rewrite ^(.+)$ /index.php?q=$1 last;
262+
}
263+
}
264+
265+
location ~ \.php$ {
266+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
267+
fastcgi_pass localhost:9000;
268+
fastcgi_index index.php;
269+
include fastcgi_params;
270+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
271+
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
272+
fastcgi_param PATH_INFO $fastcgi_path_info;
273+
}
274+
}
264275
```
265276
266277
## Deploy WordPress to AKS cluster

0 commit comments

Comments
 (0)