WordPress website on a MySQL database cluster
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 VM for WordPress.
- Create a MySQL database cluster.
- Configure Nginx web server.
- Install WordPress and additional components.
- Set up WordPress.
- Check that the website is running.
- Configure DNS.
If you no longer need the website, delete all its resources.
Before you start
- Go to the management console. Then log in to Yandex.Cloud or sign up if don't already have an account.
- On the billing page, make sure you linked a billing account, and it has the
ACTIVE
orTRIAL_ACTIVE
status. If you don't have a billing account, create one.
If you have an active billing account, you can create or select a folder to run your VM in from the Yandex.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 pricing Yandex Managed Service for MySQL).
- A fee for using a dynamic or static external IP address (see Yandex Virtual Private Cloud 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 the availability zone to host the VM in.
-
Under Images from Cloud Marketplace, click Select. Select a public image of Debian 10, Ubuntu 18.04, or CentOS 7.
-
Under Computing resources:
- Choose a platform.
- Specify the number of vCPUs and amount of RAM:
- vCPU: 2.
- Guaranteed vCPU share: 5%.
- RAM: 1 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 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:
-
Enter the username in the Login field.
-
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. Learn 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
-
On the folder page in the management console, click Create resource and select 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 Network list, select the network your VM will connect to.
- In the Name field, enter:
-
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
:
-
You can learn the IP address of the VM under Network on the VM page in the management console.
-
Connect to the VM over SSH. You can use the
ssh
tool in Linux and macOS or PuTTY for 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/UbuntuCentOS$ sudo apt-get update -qq $ 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-php72 install php php-mysql php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt $ sudo yum -y --enablerepo=remi-php72 install php-fpm $ sudo systemctl enable nginx $ sudo systemctl enable php-fpm
-
Use the Nginx configuration files to configure the web server:
DebianUbuntuCentOS-
You can edit the file 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.3-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 file 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.2-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/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/UbuntuCentOS$ wget 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/UbuntuCentOS$ sudo 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>
— with the DB name,wp-mysql-tutorial-db
.<DB_USER>
— with thewordpress
username.<DB_PASSWORD>
— with the password specified when creating a DB cluster.<DB_HOST>
— with the host in the formatc-<master ID>.rw.mdb.yandexcloud.net
. To find out the master ID, get the list of hosts via the CLI and copyCLUSTER ID
with theMASTER
role.
-
Restart Nginx and PHP-FPM:
DebianUbuntuCentOS$ sudo systemctl restart nginx.service $ sudo systemctl restart php7.3-fpm.service
$ sudo systemctl restart nginx.service $ sudo systemctl restart php7.2-fpm.service
$ sudo systemctl restart nginx.service $ sudo systemctl restart php-fpm.service
Configure WordPress
-
You can learn the IP address of the VM under Network on the VM page in the management console.
-
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 name for the website (for example,
yc-wordpress
). -
Specify the username to be used to log in to the admin panel (for example,
yc-user
). -
Enter the password to be used to log in to the admin panel.
-
Enter your email address.
-
-
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.
Check that the website is running
To check that the website is running, visit the public IP address of the VM in the browser.
Configure DNS (if you have a domain name)
To link the website to your domain, configure DNS at your registrar as follows:
- A record:
@
subdomain (use the VM's public IP address as the address). - CNAME record:
www
subdomain (use a domain with a dot at the end as the canonical name, likeexample.com).
How to delete created resources
To stop paying for your deployed website, just delete the VM wp-mysql-tutorial-web
and MySQL cluster wp-mysql-tutorial-db-cluster
.
If you reserved a static public IP address specifically for this VM:
- Open the Virtual Private Cloud in your folder.
- Go to the IP addresses tab.
- Find the address you need, click , and select Delete.