WordPress website on a MySQL database cluster
- Prepare your cloud
- Create a virtual machine for WordPress
- Create a MySQL database cluster
- Configure Nginx web server
- Install WordPress and additional components
- Complete WordPress configuration
- Configure the DNS
- Check that the website is running
- How to delete created resources
- How to create an infrastructure using Terraform
The following instructions explain how to deploy a website in WordPress CMS on a database cluster running MySQL in the Yandex Cloud infrastructure.
To set up a WordPress website based on a MySQL cluster:
- Before you start.
- Create a virtual machine for WordPress.
- Create a MySQL database cluster.
- Configure Nginx web server.
- Install WordPress and additional components.
- Complete WordPress configuration.
- Configure the DNS.
- Check that the website is running.
If you no longer need the website, delete all its resources.
You can also deploy the infrastructure for hosting a website in WordPress with a MySQL cluster via Terraform using a ready-made configuration file.
Prepare your cloud
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The cost for supporting your website in WordPress with a MySQL cluster includes:
- A fee for a continuously running VM (see Yandex Compute Cloud pricing).
- A fee for the MySQL database cluster (see Yandex Managed Service for MySQL pricing).
- A fee for using a dynamic or static external IP address (see Yandex Virtual Private Cloud pricing).
- A fee for public DNS queries and zones (see Yandex Cloud DNS pricing).
Create a virtual machine for WordPress
To create a VM:
-
On the folder page in the management console
, click Create resource and select Virtual machine. -
In the Name field, enter a name for the VM:
wp-mysql-tutorial-web
. -
Select an availability zone to put your virtual machine in.
-
Under Image/boot disk selection, go to the Cloud Marketplace tab and select Debian 11, Ubuntu 20.04 LTS, or CentOS 7 as your public image.
-
Under Computing resources:
- Choose a platform.
- Specify the number of vCPUs and amount of RAM:
- vCPU: 2.
- Guaranteed vCPU share: 20%.
- RAM: 2 GB.
-
In the Network settings section, select the network and subnet to connect the VM to. If you don't have a network or subnet, create them right on the VM creation page.
-
In the Public address field, leave the Auto value to assign the virtual machine a random external IP address from the Yandex Cloud pool, or select a static address from the list if you reserved one in advance.
-
Specify data required for accessing the VM:
-
In the Login field, enter the username, for example,
yc-user
. -
In the SSH key field, paste the contents of the public key file.
You need to create a key pair for the SSH connection yourself. See the section about how to connect to VMs via SSH.
Alert
The IP address and host name (FQDN) to connect to the VM are assigned on VM creation. If you selected No address in the Public address field, you won't be able to access the VM from the internet.
-
-
Click Create VM.
Creating the VM may take several minutes. When the VM's status changes to RUNNING
, you can proceed to the next step.
When the VM is created, it is assigned a public IP address and hostname (FQDN). This data can be used for SSH access.
Create a MySQL database cluster
To create a MySQL database cluster:
-
On the folder page in the management console
, click Create resource and select the MySQL cluster. -
In the Name field, enter the cluster name:
wp-mysql-tutorial-db-cluster
. -
Under Host class, select s2.small.
-
Under Storage size, enter 10 GB.
-
Under Database, enter:
- In the Name field, enter:
wp-mysql-tutorial-db
. - In the Username field, enter
wordpress
. - In the Password field, enter the password you will use to access the DB.
- In the Name field, enter:
-
Under Network settings:
- In the Network list, select the network your cluster will connect to.
- Under Hosts, add two more hosts in the other availability zones. When creating hosts, do not enable Public access to them.
-
Under DBMS settings, click Settings.
In the Default authentication plugin field, select mysql_native_password and click Save.
-
Click Create cluster.
Creating the DB cluster may take several minutes.
Configure Nginx web server
After the wp-mysql-tutorial-web
VM changes its status to RUNNING
:
-
Go to the VM page of the management console
. In the Network section, find the VM's public IP address. -
Connect to the VM over SSH. You can use the
ssh
utility in Linux or macOS, or PuTTY in Windows.The recommended authentication method when connecting over SSH is using a key pair. Don't forget to set up the created key pair: the private key must match the public key sent to the VM.
-
Install Nginx, PHP-FPM process manager, and additional packages:
Debian/UbuntuCentOSsudo apt-get update sudo apt-get install -y nginx-full php-fpm php-mysql sudo systemctl enable nginx
sudo yum -y install epel-release sudo yum -y install nginx sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm sudo yum -y --enablerepo=remi-php74 install php php-mysql php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt sudo yum -y --enablerepo=remi-php74 install php-fpm sudo systemctl enable nginx sudo systemctl enable php-fpm
-
Use the Nginx configuration files to configure the web server:
Debian/UbuntuCentOS-
You can edit files in the
nano
editor:sudo nano /etc/nginx/sites-available/wordpress
-
Make the file look like:
server { listen 80 default_server; root /var/www/wordpress; index index.php; server_name <DNS-server name>; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
-
Enable launch for your site:
sudo rm /etc/nginx/sites-enabled/default sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
You can edit the files
nginx.conf
andwordpress.conf
in thenano
editor:-
Open
nginx.conf
:sudo nano /etc/nginx/nginx.conf
-
Make the file look like:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; }
-
Open
wordpress.conf
:sudo nano /etc/nginx/conf.d/wordpress.conf
-
Make the file look like:
server { listen 80 default_server; root /usr/share/nginx/wordpress/; index index.php; server_name <DNS-server name>; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
-
Install WordPress and additional components
-
Download and unpack the latest WordPress version:
Debian/UbuntuCentOSwget https://wordpress.org/latest.tar.gz tar -xzf latest.tar.gz mv wordpress/wp-config-sample.php wordpress/wp-config.php sudo mv wordpress /var/www/wordpress sudo chown -R www-data:www-data /var/www/wordpress
curl https://wordpress.org/latest.tar.gz --output latest.tar.gz tar -xzf latest.tar.gz mv wordpress/wp-config-sample.php wordpress/wp-config.php sudo mv wordpress /usr/share/nginx/wordpress sudo chown -R nginx:nginx /usr/share/nginx/wordpress/
Change the SELinux settings:
sudo semanage fcontext -a -t httpd_sys_content_t "/usr/share/nginx/wordpress(/.*)?" sudo semanage fcontext -a -t httpd_sys_rw_content_t "/usr/share/nginx/wordpress(/.*)?" sudo restorecon -R /usr/share/nginx/wordpress sudo setsebool -P httpd_can_network_connect 1
-
Get WordPress security keys:
curl -s https://api.wordpress.org/secret-key/1.1/salt/
Save the command output. You will need the keys in the next step.
-
Add the security keys to the WordPress configuration file
wp-config.php
. You can edit files in thenano
editor:Debian/UbuntuCentOSsudo nano /var/www/wordpress/wp-config.php
sudo nano /usr/share/nginx/wordpress/wp-config.php
Replace the configuration section for the values from the previous step:
define('AUTH_KEY', 't vz,|............R lZ5]'); define('SECURE_AUTH_KEY', '@r&pPD............dK-A%='); define('LOGGED_IN_KEY', '%6TuLl............9>/dNE'); define('NONCE_KEY', 'DO(u.H............$?ja-e'); define('AUTH_SALT', '|G Vo<............Xeb.~y'); define('SECURE_AUTH_SALT', 'Y5tIYA............7Lxf8J'); define('LOGGED_IN_SALT', 'gR]>WZ............<>|;YY'); define('NONCE_SALT', '=]nQIb............HLT2:9');
-
Go to the cluster connection configuration section
wp-mysql-tutorial-db-cluster
:// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', '<DB_NAME>' ); /** MySQL database username */ define( 'DB_USER', '<DB_USER>' ); /** MySQL database password */ define( 'DB_PASSWORD', '<DB_PASSWORD>' ); /** MySQL hostname */ define( 'DB_HOST', '<DB_HOST>' );
Replace the placeholders in the file:
-
<DB_NAME>
: DB name,wp-mysql-tutorial-db
. -
<DB_USER>
:wordpress
username. -
<DB_PASSWORD>
: password you set when creating a DB cluster. -
<DB_HOST>
: MySQL host name inXXXX-XXXXXXXXXX.mdb.yandexcloud.net
format.To find out the FQDN of your MySQL host:
Management consoleCLI- Go to the MySQL cluster page.
- In the Databases tab, click
- Select Connect.
- Locate the line
mysql --host=XXXX-XXXXXXXXXX.mdb.yandexcloud.net
, whereXXXX-XXXXXXXXXX.mdb.yandexcloud.net
is the FQDN of theMASTER
host.
Get a host list and copy the
NAME
of theMASTER
host:yc managed-mysql host list --cluster-name <MySQL cluster name>
+-----------------------------+----------------------+---------+--------+---------------+-----------+ | NAME | CLUSTER ID | ROLE | HEALTH | ZONE ID | PUBLIC IP | +-----------------------------+----------------------+---------+--------+---------------+-----------+ | rc1a-...mdb.yandexcloud.net | c9quhb1l32unm1sdn0in | MASTER | ALIVE | ru-central1-a | false | | rc1b-...mdb.yandexcloud.net | c9quhb1l32unm1sdn0in | REPLICA | ALIVE | ru-central1-b | false | +-----------------------------+----------------------+---------+--------+---------------+-----------+
-
-
Restart Nginx and PHP-FPM:
Debian/UbuntuCentOSsudo systemctl restart nginx.service sudo systemctl restart php7.4-fpm.service
sudo systemctl restart nginx.service sudo systemctl restart php-fpm.service
Complete WordPress configuration
-
Go to the VM page of the management console
. In the Network section, find the VM's public IP address. -
Open the VM by entering its address in your browser.
-
Select the language and click Continue.
-
Fill out information to access the website:
- Enter any website name, for example,
wp-your-project
. - Specify the username to be used to log in to the admin panel (for example,
admin
). - Enter the password to be used to log in to the admin panel.
- Enter your email address.
- Enter any website name, for example,
-
Click Install WordPress.
-
If the installation is successful, click Log in.
-
Log in to the website with the username and password specified in the previous steps. This opens the admin panel, where you can start working with your website.
Configure the DNS
If you have a registered domain name, use the Cloud DNS service to manage the domain.
You can also configure the DNS via Terraform. For more information, see How to create an infrastructure using Terraform.
The tutorial below describes configuring DNS for the example.com
domain name.
Add a zone
To add a public zone:
- Open the Cloud DNS section of the folder where you need to create a DNS zone.
- Click Create zone.
- Specify the zone settings:
- Zone name:
example-zone-1
. - Zone:
example.com
. Specify your registered domain. - Type: Public.
- Zone name:
- Click Create.
Add resource records
Create DNS records in the public zone:
- Under Network on the VM page in the management console
find the VM's public IP address. - Create an A record:
- Open the Cloud DNS section of the folder where the
example.com
zone is located. - Select
example.com
from the list. - Click Create record.
- Set the record parameters:
- Name: Leave empty.
- Record type: Keep
A
as the value. - TTL (record time to live): Leave the default.
- Value: Enter your VM's public address.
- Click Create.
- Open the Cloud DNS section of the folder where the
- Create a CNAME record:
- Select
example.com
from the list. - Click Create record.
- Set the record parameters:
- Name:
www
. - Record type: Select
CNAME
as the value. - TTL (record time to live): Leave the default.
- Value: Enter
example.com
.
- Name:
- Click Create.
- Select
Delegate the domain name
Delegation is the transfer of authority from the registrar's servers to yours. For a domain, NS resource records are created (ns1.yandexcloud.net
and ns2.yandexcloud.net
).
To delegate a domain, specify its DNS servers in the registrar's account.
Delegation does not take effect immediately. It normally takes internet service providers up to 24 hours (86400 seconds) to update records. This depends on the TTL value which determines how long domain records are cached.
You can verify domain delegation using the Whoisdig
utility:
dig +short NS example.com
Result:
ns2.yandexcloud.net.
ns1.yandexcloud.net.
Check that the website is running
To check that the site is up, enter its IP address or domain name in your browser:
http://<public_IP_of_VM>
.http://www.example.com
.
To access the WordPress control panel, use http://www.example.com/wp-admin/
.
How to delete created resources
To stop paying for the deployed website:
If you reserved a static public IP address for the VM, delete it.
How to create an infrastructure using Terraform
With Terraform
For more information about the provider resources, see the documentation on the Terraform
If you change the configuration files, Terraform automatically determines which part of your configuration is already deployed and what should be added or removed.
To host a WordPress website based on a MySQL cluster via Terraform:
-
Install Terraform, get the authentication credentials, and specify the source for installing the Yandex Cloud provider (see Configure a provider, step 1).
-
Prepare files with the infrastructure description:
Ready-made archiveCreating files manually- Create a directory for files:
- Download the archive
(1 KB). - Unpack the archive to the directory. The
wordpress-mysql.tf
file should be added to the directory.
-
Create a directory for files:
-
Create the
wordpress.tf
configuration file in the directory:wordpress-mysql.tfterraform { required_providers { yandex = { source = "yandex-cloud/yandex" version = ">= 0.47.0" } } } provider "yandex" { zone = "ru-central1-a" } resource "yandex_compute_disk" "boot-disk" { name = "bootvmdisk" type = "network-hdd" zone = "ru-central1-a" size = "20" image_id = "<image_ID>" } resource "yandex_compute_instance" "vm-wordpress-mysql" { name = "wp-mysql-tutorial-web" platform_id = "standard-v3" zone = "ru-central1-a" resources { core_fraction = 20 cores = 2 memory = 2 } boot_disk { disk_id = yandex_compute_disk.boot-disk.id } network_interface { subnet_id = yandex_vpc_subnet.subnet-1.id security_group_ids = ["${yandex_vpc_security_group.sg-1.id}"] nat = true } metadata = { ssh-keys = "<username>:<SSH_key_contents>" } } resource "yandex_mdb_mysql_cluster" "wp-cluster" { name = "wp-mysql-tutorial-db-cluster" environment = "PRESTABLE" network_id = yandex_vpc_network.network-1.id version = "8.0" security_group_ids = ["${yandex_vpc_security_group.sg-1.id}"] resources { resource_preset_id = "s2.small" disk_type_id = "network-ssd" disk_size = "10" } host { zone = "ru-central1-a" subnet_id = yandex_vpc_subnet.subnet-1.id assign_public_ip = false } host { zone = "ru-central1-b" subnet_id = yandex_vpc_subnet.subnet-2.id assign_public_ip = false } host { zone = "ru-central1-d" subnet_id = yandex_vpc_subnet.subnet-3.id assign_public_ip = false } } resource "yandex_mdb_mysql_database" "wp-db" { cluster_id = yandex_mdb_mysql_cluster.wp-cluster.id name = "wp-mysql-tutorial-db" } resource "yandex_mdb_mysql_user" "wp-user" { cluster_id = yandex_mdb_mysql_cluster.wp-cluster.id name = "wordpress" password = "password" authentication_plugin = "MYSQL_NATIVE_PASSWORD" permission { database_name = yandex_mdb_mysql_database.wp-db.name roles = ["ALL"] } } resource "yandex_vpc_security_group" "sg-1" { name = "wordpress" description = "Description for security group" network_id = yandex_vpc_network.network-1.id ingress { protocol = "TCP" description = "ext-http" v4_cidr_blocks = ["0.0.0.0/0"] port = 80 } ingress { protocol = "TCP" description = "ext-ssh" v4_cidr_blocks = ["0.0.0.0/0"] port = 22 } ingress { protocol = "TCP" description = "ext-msql" v4_cidr_blocks = ["0.0.0.0/0"] port = 3306 } ingress { protocol = "TCP" description = "ext-https" v4_cidr_blocks = ["0.0.0.0/0"] port = 443 } egress { protocol = "ANY" description = "any" v4_cidr_blocks = ["0.0.0.0/0"] } } resource "yandex_vpc_network" "network-1" { name = "network1" } resource "yandex_vpc_subnet" "subnet-1" { name = "subnet1" zone = "ru-central1-a" network_id = yandex_vpc_network.network-1.id v4_cidr_blocks = ["192.168.1.0/24"] } resource "yandex_vpc_subnet" "subnet-2" { name = "subnet2" zone = "ru-central1-b" network_id = yandex_vpc_network.network-1.id v4_cidr_blocks = ["192.168.2.0/24"] } resource "yandex_vpc_subnet" "subnet-3" { name = "subnet3" zone = "ru-central1-d" network_id = yandex_vpc_network.network-1.id v4_cidr_blocks = ["192.168.3.0/24"] } resource "yandex_dns_zone" "zone-1" { name = "example-zone-1" description = "Public zone" zone = "example.com." public = true } resource "yandex_dns_recordset" "rs-1" { zone_id = yandex_dns_zone.zone-1.id name = "example.com." ttl = 600 type = "A" data = ["${yandex_compute_instance.vm-wordpress-mysql.network_interface.0.nat_ip_address}"] } resource "yandex_dns_recordset" "rs-2" { zone_id = yandex_dns_zone.zone-1.id name = "www" ttl = 600 type = "CNAME" data = ["example.com"] }
For more information about the parameters of resources used in Terraform, see the provider documentation:
-
Under
metadata
, enter the metadata for creating a VM instance:<username>:<SSH_key_contents>
. Regardless of the username specified, the key is assigned to the user set in the image configuration. In different images, these users differ. For more information, see Keys processed in public images. -
Under
boot_disk
, specify the ID of a VM image with a relevant set of components: -
Create resources:
-
In the terminal, change to the folder where you edited the configuration file.
-
Make sure the configuration file is correct using the command:
terraform validate
If the configuration is correct, the following message is returned:
Success! The configuration is valid.
-
Run the command:
terraform plan
The terminal will display a list of resources with parameters. No changes are made at this step. If the configuration contains errors, Terraform will point them out.
-
Apply the configuration changes:
terraform apply
-
Confirm the changes: type
yes
in the terminal and press Enter.
-