In this post, we cover how to set up your new WordPress site by installing a LAMP stack.
What is a LAMP Stack?
To put simply, a LAMP stack is an acronym.
- The L stands for “Linux”, your server’s operating system.
- The A stands for “Apache”, your web server software.
- The M stands for “MySQL”, your web server’s database management system.
- Lastly, the P stands for “PHP”, a scripting language used by WordPress.
Put together, you have a LAMP stack.
On a VPS, the LAMP stack provides the basic functionality needed to host WordPress on your new site. Every website needs an operating system, web server software, a database, and PHP. In order to follow along, you will need to have a Linux operating system on your server (preferably Ubuntu as we use Ubuntu in the tutorial). The operating system should have already been done when you purchased the VPS.
How to Install a LAMP Stack
To begin, you will need to be signed into your VPS. We will assume you already had experience with signing in to your new server. If you’re on a Windows machine, you can sign into the server with a tool such as PuTTY. If you’re on a Mac or running some Linux distro, you can simply use the Terminal. Once you’re signed into the server, we will install Apache2.
To begin, simply type the following command in your terminal:
sudo apt update
Notice the sudo, that indicates you will need root privileges and will prompt you for your password. The above command will update Ubuntu’s package manager, apt. Now let’s install Apache2:
sudo apt install apache2
The package manager will tell you the packages that will be installed along with how much disk space that will be used, and if you wish to proceed. Enter Y and press enter to proceed with the installation.
Once the packages are done installing, you can verify Apache has been installed by typing your server’s IP address in a web browser:
You will see Ubuntu’s default web page:
Congratulations! Apache is now installed. Next, let’s install MySQL.
We will use apt again to install MySQL with the following command:
sudo apt install mysql-server
Again, the package manager will list all of the packages that will be installed along with the amount of disk space the installation will take up. Enter Y and press Enter to continue with the installation.
MySQL is now installed. However, by default, the root user will have no password. Let’s change the password for better security practices. Type the following command to change the root password:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Replace MyNewPass with a secure password of your choosing. You can now exit the MySQL command prompt by typing the command:
Congratulations! We now have MySQL installed, so WordPress can properly communicate with the database. Now, let’s install the last piece of the LAMP stack – PHP.
PHP can be installed by typing the following command, again, we will use the package manager:
sudo apt install php libapache2-mod-php php-mysql
The command will install helper packages so that PHP can work with both Apache and MySQL.
By default, Apache will serve files by first looking for index.html. However, WordPress is primarily composed of PHP files, so we don’t want that behavior. Let’s tell Apache to serve PHP files before the HTML files. To do that, type the following command:
sudo nano /etc/apache2/mods-enabled/dir.conf
The command will open the nano editor, and present the following file:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
Let’s move the index.php to replace index.html to ensure PHP files will be displayed first. The file should now look like the following:
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>
The file can be saved within nano by entering CTRL+X. Confirm the changes by entering Y and hit Enter to save the file to the same location. Once that is done, then we will want to restart Apache:
sudo systemctl restart apache2
We now have installed all components needed for a proper LAMP stack. Let’s ensure that PHP is working properly.
Let’s create a PHP file in the /var/www/html/ directory and ensure PHP is working properly. Enter the following command:
sudo vi /var/www/html/php-info.php
Add the following code to the file:
<?php phpinfo(); ?>
Once the code is added, press :wq to apply the changes and save the file. Now, open the web browser and enter the following URL:
You will now see the PHP configuration file. Once you have verified the page loads, we will want to delete the file for security reasons. The file can be deleted by typing the command:
sudo rm php-info.php
We now have the core components needed for running a WordPress site!