LAMP refers to using a combination of Linux, Apache, MySQL, and PHP to host websites that have dynamic content or web applications. This collection uses Apache as a web service, PHP to handle dynamic content, and MySQL to store data and serve as a database.
Requirements
You will need a server running the Ubuntu 18.04 operating system and a user having sudo permissions for this tutorial.
How to Install LAMP?
Your first step will be installing the Apache web server. To install this program, you can use apt. First, you must update the repositories which apt uses:
sudo apt-get update
Next, install apache with the following command:
sudo apt-get install apache2
Once you have installed this program, the service will automatically start. You can enter your instance IP address in the browser to view the default Apache page. Its default screen is as shown below:
When the steps of installation are correctly performed, but you are not seeing the default screen, use the following command to check the status of the service first:
sudo systemctl status apache2
If the output of the command is as shown in the image below, it indicates that the service was not started properly:
Enter the following command to start the service and re-check the status of the service:
sudo systemctl start apache2
When the service is properly set up, and you still are not seeing the default page, chances are that the firewall configured on the instance is blocking traffic to the web server. Assuming you are using ufw as your firewall, type the following command to allow incoming web server traffic:
sudo ufw allow in "Apache Full"
Now, restart the Apache service using the following command. After you have run this command, the default Apache screen can be seen:
sudo systemctl restart apache2
The next step is to install MySQL. You can use apt to install this program as well:
sudo apt-get install mysql-server
You can run a script with the following command after the installation is complete. By running this script, you will remove default settings that can pose security problems:
sudo mysql_secure_installation
The first section will ask you if you would like to set up the VALIDATE PASSWORD plugin. With this plugin, you can determine the security of the passwords you choose for MySQL. You can press the y key to set up the plugin, and any other key to not change the current settings (if you want to set up the plugin, you will be prompted for the amount of accepted credit):
You will be asked to choose a password for root in the next step. This user will be similar to the root user in Linux concerning importance and access; for this reason, the password you will choose for the root must be sufficiently strong.
The subsequent questions will be used to remove unknown users or modify the default settings. Answer y to all the following questions:
You can now install PHP and any plugins you need to allow PHP, MySQL, and Apache to communicate with each other using the following command:
sudo apt install php libapache2-mod-php php-mysql
Note: To use multiple different domains in the instance, you must configure the Virtual Host settings. To do this, at least one domain name that is configured for the instance is required.
You can see how to make these settings in the following. For this tutorial, we will use the example.com domain. Use your domain name instead of it in all virtual host settings.
By default, the Apache web server will display the files in the var/www/html/ path. To begin, use the following command to create a similar path for your domain:
sudo mkdir /var/www/example.com
Then, run the following command. With this command, you will change the ownership of the above path to the USER variable value:
sudo chown -R $USER:$USER /var/www/example.com
Use the following command to set the required permissions:
sudo chmod -R 755 /var/www/example.com
The next thing you should do is to create a file like the default one, which can be found at var/www/html/. This is the file that you will see after setup is complete and when you search for your domain name:
nano /var/www/example.com/index.html
Insert the following text into the file you created. Then save the file and exit:
<html> <head> <title>Welcome to example.com!</title> </head> <body> <h1>your setup is working!</h1> </body> </html>
Now you have to create a virtual-host file that points to the file you created in the previous step. Use the Apache sample file to do this:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Change the values of the ServerAdmin and DocumentRoot variables in the new file. The ServerAdmin variable value has to be the email address that the administrator will have access to, and the DocumentRoot value must be the address of the file you created earlier, which is var/www/example.com/:
Now, activate the created file by using the following command:
sudo a2ensite example.com.conf
Running the following command will deactivate the default file:
sudo a2dissite 000-default.conf
To verify that the settings are correct, use the following command. Assuming the settings are correct, the output of the command will be like the following image:
sudo apache2ctl configtest
If so, restart the Apache service to apply the settings:
sudo systemctl restart apache2
When you enter the configured domain name into the browser, you can see the message that is written in the file.
To verify that the stack settings are correct, consider writing a simple script in php language. Once you have configured the domain name and virtual host settings, you can insert this script into the var/www/example.com/ path, or into the default path otherwise, i.e. var/www/html/. You can use, for example, the following script (create the script with the name info.php in the path you want):
<?php phpinfo (); ?>
Now, when you enter the address http://example.com/info.php (with the virtual host settings) or http://your_IP/info.php, you will see the following page:
Note that if you have followed the settings regarding the virtual host and used the default path for the above settings, then you won’t see the displayed page since the default path settings are disabled, and you must re-enable the default mode:
sudo a2ensite 000-default.conf sudo systemctl restart apache2