WordPress is an open-source content management system(CMS) that empowers users to effortlessly create websites, blogs or online shopping malls. Officially it provide various plans, including a free option, to cater to diverse CMS development needs. Numerous hosting services present plans ranging from \(2~\)40 per month for hosting WordPress. Additionally, several cloud vendors offer free trials for computing instances, allowing users to explore self-hosting WordPress.
For beginners, experimenting with free hosting options is a great way to familiarize oneself with the platform. However, for those seeking a stable, low-latency website, it is advisable to consider subscribing to premium plans or renting a Virtual Private Server (VPS) for hosting.
In this context, I will share my experience in building a WordPress website on the Debian system hosted on my VPS.
Please note that using docker-compose could provide a simpler approach compared installing all the dependencies manually as outlined below. Nevertheless, engaging in the entire process will enhance your understanding of the underlying dependencies.
Install and set up PHP, MariaDB, phpMyAdmin
1. First, install PHP
sudo apt update
# Install required PHP packages
sudo apt -y install php php-cgi php-mysqli php-pear php-mbstring libapache2-mod-php php-common php-phpseclib php-mysql
2. Install MariaDB
sudo apt install mariadb-server mariadb-client -y
# initiate secure installation
sudo mysql_secure_installation
# Follow on-screen prompts: skip unix_socket auth, set root password, remove anonymous users, disallow root remote login, remove test database, reload privileges.
#- Switch to unix_socket authentication [Y/**n**] - Enter n to skip.
#- Set root password? [**Y**/n] - Type y. enter your password and press Enter.
#- Remove anonymous users? [**Y**/n] - Type y and press Enter.
#- Disallow root login remotely? [**Y**/n] - Type y and press Enter.
#- Remove test database and access to it? [**Y**/n] - Type y and confirm with Enter.
#- Reload privilege tables now? [**Y**/n] - Type y and confirm with Enter.
3. Set up MariaDB database and user
sudo mysql -u root -p
MariaDB [(none)]> CREATE DATABASE yourdb;
MariaDB [(none)]> CREATE USER 'youuser'@localhost IDENTIFIED BY 'yourpassword';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'youuser'@localhost IDENTIFIED BY 'yourpassword';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit
4. Install PHPMyAdmin
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
# Import keyring and verify
wget https://files.phpmyadmin.net/phpmyadmin.keyring
gpg --import phpmyadmin.keyring
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz.asc
gpg --verify phpMyAdmin-latest-all-languages.tar.gz.asc
# Setup and configure PHPMyAdmin
sudo mkdir /var/www/html/phpMyAdmin
sudo tar xvf phpMyAdmin-latest-all-languages.tar.gz --strip-components=1 -C /var/www/html/phpMyAdmin
mv /var/www/html/phpMyAdmin/config.sample.inc.php /var/www/html/phpMyAdmin/config.inc.php
sudo vim /var/www/html/phpMyAdmin/config.inc.php
# update the value of $cfg['blowfish_secret'] = '' to 'your_passpharase', and save
sudo chmod 660 /var/www/html/phpMyAdmin/config.inc.php
sudo chown -R www-data:www-data /var/www/html/phpMyAdmin
sudo systemctl restart apache2
5. Configure Firewall
If your VPS has security rules like firewall, you should update it to allow traffic on ports 80 and 443. If you have firewall in the system, you could configure it like this:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
Now you should be able to visit http://your_ip_of_VPS/phpMyAdmin/
Install WordPress
1. Download and install wordpress
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo mv wordpress /var/www/html/
cp wp-config-sample.php wp-config.php
sudo chmod 660 /var/www/html/wordpress/wp-config.php
sudo chown -R www-data:www-data /var/www/html/wordpress
2. Configure Apache
Using vim to create and edit the configuration.
vim /etc/apache2/sites-available/wordpress.conf
We set a rewrite rule on 80 port, and setup the document root and some directory rules on 443 port.
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{SERVER_NAME} =2volt.cc
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerName 2volt.cc
DocumentRoot /var/www/html/wordpress
<FilesMatch "\.(?:cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<Directory /var/www/html/wordpress>
Options FollowSymLinks
AllowOverride Limit Options FileInfo
DirectoryIndex index.php
Require all granted
</Directory>
<Directory /var/www/html/wordpress/wp-content>
Options FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
Now we should enable the site
a2ensite wordpress
And then restart apache2
sudo systemctl restart apache2
3. Configurate wp-config.php
cd /var/www/html/wordpress
vim wp-config.php
Update DB_NAME,DB_USER,DB_PASSWORD
define( 'DB_NAME', 'yourdb' );
/** Database username */
define( 'DB_USER', 'youruser' );
/** Database password */
define( 'DB_PASSWORD', 'yourpassword' );
4. Set up SSL
We will use certbot to get free certificate from https://letsencrypt.org/ and then update our configuration automatically using following commands. After that we restart Apache2 to apply changes.
sudo apt install snapd
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --apache
sudo systemctl restart apache2
Now you should be able to open https://your_ip_of_VPS/
. and you can following the Guide on it to finish the setting up.