WordPress is obviously one of the most popular Content Management Systems (CMS) available in the market. There are multiple ways in which you can install WordPress – one of them is using LAMP. This method allows you to host your very own WordPress website on a VPS. In this tutorial, you will learn about installing WordPress on Ubuntu 18.04.
Many of them use this method as it offers full control of WordPress backend for experimentation and advanced uses.
Also, users mostly keep LAMP stack for their WordPress development environment.
LAMP refers to Linux, Apache, MySQL, and PHP. Linux operating system comes with Apache web server and MySQL Database that uses PHP to process dynamic website content.
In this tutorial, you will learn to install WordPress on Ubuntu 18.04 using LAMP stack.
Also, note that before you install WordPress on Ubuntu, you need to access your VPS using SSH.
Install and Configure Apache 2
LAMP uses the popular and widely used, Apache web server 2. Ubuntu’s package manager helps to install Apache as shown below:
sudo apt update sudo apt install apache2
These commands need you to provide passwords. On the command prompt, press Y and hit ENTER to continue the installation.
By default, the UFW firewall is available in all the Ubuntu installations. The UFW firewall allows traffic via HTTP as well as HTTPS. If you aren’t using UFW, skip this step. Use the below command to check the UFW application profiles:
sudo ufw app list
This will provide the below output:
Available applications: Apache Apache Full Apache Secure OpenSSH
Execute the below command to allow HTTP and HTTPS traffic for Apache Full profile:
sudo ufw allow in "Apache Full"
To confirm if this has worked, check this profile information by using:
sudo ufw app info "Apache Full"
In order to verify if Apache was installed correctly, you will need the public IP address. Use the below command to get it:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
You will be provided with 2-3 IPs using this command. If you want to try all of this, use http://your_public_IPAddress on a browser. The default Ubuntu Apache page will get displayed with this.
Install MySQL
After the web server starts running, you can install the MySQL database. This can be done using the below command:
sudo apt install mysql-server
This command will ask for a password. After starting the installation, based on your preference you can press Y and ENTER to complete the installation. To open MySQL terminal use the below command:
sudo mysql
Set a password for the root user using the command below:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password;
To verify these changes, use Flush command as shown below:
mysql> FLUSH PRIVILEGES;
To come back from the MySQL prompt, type exit. With this the MySQL installation gets completed.
Install PHP
For displaying dynamic content, you need PHP which can be installed by using:
sudo apt install php libapache2-mod-php php-mysql
Use the below command to install additional PHP extensions for WordPress:
sudo apt install php-curl php-gd php-xml php-mbstring php-xmlrpc php-zip php-soap php-intl
The index.html gets displayed as a default setting, when you request for a directory. If you want to show index.php instead of index.html, open the dir.conf file using the vi editor as below:
sudo vi /etc/apache2/mods-enabled/dir.conf
It will appear as below:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
Swap the positions of index.html and index.php and save the file.
To make the changes visible, restart the Apache server using:
sudo systemctl restart apache2
For PHP testing, you can create a sample PHP file sample.php and add the below lines of code:
Add this file to the Web Root of Apache which is located at – /var/www/html/
After saving this, try to access this page by using http://Your_Public_IP/Sample.php.
MySQL Setup for WordPress
Login into MySQL as a root user. For this, use the command below:
mysql -u root -p
You will be prompted to type the password for root that is set up earlier. After logging in, it is possible to create a separate database for WordPress.
Below is a new DB created called WordPressDB:
mysql> CREATE DATABASE WordPressDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Now, create a separate user for WordPress with the name WordPressUser. For creating a new user and granting privileges, use the command below:
mysql> GRANT ALL ON WordPressDB.* TO ' WordPressUser '@'localhost' IDENTIFIED BY 'NewPasswordToBeSet';
Give a strong password in place of NewPasswordToBeSet. To verify these changes, use the command:
mysql> FLUSH PRIVILEGES;
After it gets completed, exit the MySQL prompt.
Prepare to Install WordPress on Ubuntu
Create a configuration file, for example :WordPress.conf. Locate it to /etc/apache2/sites-available/. This will be a copy of the default configuration file that is already present in this location.
Additionally, create a WordPress directory (or provide any other name at location /var/www/). The complete location would appear as /var/www/wordpress.
For testing this, the file WordPress.conf will be the Apache configuration file. In the file, you can enable .htaccess by including these lines to the VirtualHost block:
<Directory /var/www/wordpress /> AllowOverride All </Directory>
After editing this, save the file.
Next, enable the mod_rewrite to use WordPress permalink feature. You can do it with the below command:
sudo a2enmod rewrite
In the file /etc/apache2/apache2.conf, change the ServerName directive by offering the server’s IP or hostname.
This complete configuration can be tested by using the below command:
sudo apache2ctl configtest
For verifying the changes done, restart the Apache server using the previously mentioned restart command:
sudo systemctl restart apache2
Configure and Install WordPress on Ubuntu
You can change your location to a directory and start downloading WordPress. This can be done as shown below:
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
Use the below command to extract this file:
tar xzvf latest.tar.gz
Now you can create a .htaccess file:
vi /tmp/wordpress/.htaccess
Save the file and rename the wp-config-sample.php file using the command:
mv /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
Create the below folder at the given location:
mkdir /tmp/wordpress/wp-content/upgrade
With this, the initial setup gets completed which can be copied to the document root.
sudo cp -a /tmp/wordpress/. /var/www/wordpress
To check if everything works correctly, you will have to change the ownership of WordPress files to the www-data users and groups. These users are those that Apache web server will use.
For changing the ownership, you can use this command:
sudo chown -R www-data:www-data /var/www/wordpress
Also, it is important to set the correct permission to the directories and files:
sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;
For the initial configuration, it requires the WordPress salt to be generated. This can be done using:
curl -s https://api.wordpress.org/secret-key/1.1/salt/
This will give a different output every time and will comprise of a list of salt values. The output of the above command needs to be copied and added to the wp-config.php file.
vi /var/www/wordpress/wp-config.php
Replace the existing dummy values within this file. Save the file after making the changes.
The wp-config.php file contains some Database configuration at the top. Replace the DB_NAME, DB_USER, DB_PASSWORD with values you have created for WordPress.
define('DB_NAME', 'WordPressDB'); /** MySQL database username */ define('DB_USER', 'WordPressUser'); /** MySQL database password */ define('DB_PASSWORD', 'DB_Password'); Also, you can add the file system method at the very bottom, as shown below: define('FS_METHOD', 'direct');
Saving this file completes the backend setup. Now WordPress can be accessed via the interface by using the URL http://Your_IP_Address.
Next settings will be similar to the regular WordPress setup. First, you will be asked to select a language.
Then you will be redirected to a page where you can set the username and password.
Make sure that you change the username and password for enhanced WordPress security. After clicking the Install WordPress button, you will complete your WordPress setup and will be directed to a login screen.
Here enter the WordPress user name and password to access the dashboard.
Wrap Up
Now you have learned to install WordPress on Ubuntu 18.04 with the LAMP stack. From here you can start discovering all the amazing WordPress features and develop some amazing websites.