WordPress is the most popular CMS out there with over 39% of the sites powered by it. There are multiple servers on which you can install WordPress. One of the most popular options is by using LAMP stack. In this tutorial, I will show you how to install WordPress on LAMP stack in Ubuntu 18.04.
LAMP stands for Linux, Apache, MySQL and PHP.
1) Installing and Configuring Apache2
Apache2 is the webserver that we will be using. You can install it on your server with the following commands:
sudo apt update
sudo apt install apache2
Hit Y to install the whole apache2 server. Next step is to allow traffic in the ufw firewall. We can do this with the help of the following command:
sudo ufw allow in "Apache Full"
We can verify if the rules have been added by using the following command:
sudo ufw app info "Apache Full"
With this, you have successfully installed Apache on your server. Next, we need to install MySQL.
2) Install MySQL
We will install MySQL with the following command:
sudo apt install mysql-server
Press Y for the installation to start. After that, we will set the root password. We will do that by opening MySQL:
sudo mysql
After that, use the following command to set root password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password;
Now, flush privilege by using the following command:
mysql> FLUSH PRIVILEGES;
Input exit and hit enter to get out of MySQL.
3) Install PHP
PHP is required for WordPress as it’s written in PHP. We can install PHP with the following command:
sudo apt install php libapache2-mod-php php-mysql
WordPress needs some additional PHP extensions. Install them with the following command:
sudo apt install php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl
Next, we have to change the precedence of .php extension. To do this, open the dir.conf file in any text editor:
sudo nano /etc/apache2/mods-enabled/dir.conf
The file will look as shown below:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Bring index.php to the front. Then save the file. After that, restart Apache with the new configuration:
sudo systemctl restart apache2
To test PHP, create a new file called phpinfo.php in the root directory (/var/www/html). Then access this page by visiting http://server-ip/phpinfo.php. If PHP Info page appears, it means PHP is installed and is working correctly.
MySQL Setup for WordPress
For WordPress, we need to create a new user and a new database in MySQL. We can do this by entering MySQL console. Do this by typing in the following:
mysql -u root -p
After this, type in the password you had set for MySQL root user. After that, create a new database for WordPress:
mysql> CREATE DATABASE WordPressDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Next, we will create an user for this database as shown below:
mysql> GRANT ALL ON WordPressDB.* TO ' WordPressUser '@'localhost' IDENTIFIED BY 'NewPasswordToBeSet';
Then, we will flush the privileges:
mysql> FLUSH PRIVILEGES;
After doing this, exit MySQL.
Configure Apache for WordPress
We will create a custom configuration file (let’s say, WordPress.conf). Also, we will create a directory for WordPress at /var/www/ (let’s say, wordpress). Now we will add the following lines to the WordPress.conf file:
<Directory /var/www/wordpress/>
AllowOverride All
</Directory>
After adding these lines, save the file. We also need to enable mod_rewrite to use the permalinks functionality. We can do this by executing the following:
sudo a2enmod rewrite
Next, we will change the ServerName directive in the etc/apache2/apache2.conf file. Change it to either the server IP or the server hostname. You can check the config by typing:
sudo apache2ctl configtest
if it gives the Syntax OK message, then restart Apache server:
sudo systemctl restart apache2
ALSO READ – 10 Ways to Secure Your WordPress Site
Download and Install WordPress
Finally, we will download and install WordPress. First, let’s switch to tmp folder and download WordPress:
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
Extract the file:
tar xzvf latest.tar.gz
Next, we will create .htaccess file:
/tmp/wordpress/.htaccess
Rename wp-sample.config.php to wp-config.php:
mv /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
Create the upgrade folder:
mkdir /tmp/wordpress/wp-content/upgrade
Finally, copy the files to the web root:
sudo cp -a /tmp/wordpress/. /var/www/wordpress
To make sure WordPress works as expected, we need to fix file permissions and file ownership. To change the ownership, use the following command:
sudo chown -R www-data:www-data /var/www/wordpress
After that, set the correct file and folder permission:
sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;
Generate WordPress salt with the following command:
curl -s https://api.wordpress.org/secret-key/1.1/salt/
Add the generated values to the wp-config.php file:
nano /var/www/wordpress/wp-config.php
Then, we need to enter the database details:
define('DB_NAME', 'WordPressDB');
/** MySQL database username */
define('DB_USER', 'WordPressUser');
/** MySQL database password */
define('DB_PASSWORD', 'DB_Password');
Also, add the following at the bottom of the file:
define('FS_METHOD', 'direct');
Save the wp-config.php file.
Now, enter your server IP or domain name pointed to that your server. WordPress installation wizard will start. Complete it to finish installing WordPress.
For SSL, you can see the guide on WordPress installation on OpenLiteSpeed server.
Congratulations! You have successfully installed WordPress on LAMP stack on Ubuntu 18.04. If you have any queries or suggestions, then please let me know in the comments down below.
ALSO READ – How to Install WordPress on AWS Lightsail