What Is A Lamp Stack?
A LAMP stack is an open-source stack that combines four services that the developers use to create powerful websites and applications. The base layer is the operating system called Linux, the layer for the web server is Apache, the database layer uses MySQL, and PHP is used as the programming language. When used properly, these four layers enable hosting, creating, and maintaining websites and web applications.
LAMP Stack Architecture
Linux
Linux is the operating system and the first layer of the architecture. It binds every other layer together. Linux is also an open-source operating system; it can be configured to meet the requirements of other software layers.
Apache
Apache is used for the web server and is the second layer of the LAMP stack model. Apache uses an HTTP connection to exchange information between the browser and the web server. Apache also calls upon PHP to serve dynamic content to the web server.
MySQL
MySQL is the third layer in our LAMP stack. MySQL is used for storing and managing information. For example, client data and product data are all stored in the relational, open-source MySQL software. When information is requested, the database is queried and served.
PHP
Sitting on top of them all is the fourth and final layer. PHP stands for PHP i.e. Hypertext Preprocessor, and it's a scripting language that allows you to dynamically serve content. Dynamic content is content whose values are not constant. They change depending on the circumstances of the function they are trying to accomplish. PHP is also linked to MySQL and the web server, which then is used in tandem to serve content to your browser.
How It Works
Whenever you open a website through a browser, the LAMP stack is triggered, and the information it processes goes through the following flow. The web application requests the web browser. The LAMP stack then initiates the Apache web server and MySQL, which use PHP for their communication.
The Apache web server first receives the request from the web browser, depending on the request (static or dynamic content) it serves it accordingly. If the request is for static content, Apache serves it immediately. However, if it is dynamic content, the PHP component then gets involved and loads the correct PHP file to process that request.
Once the correct PHP file is found, the written functions within it are then used to interpret the request and provide the necessary output. Some PHP functions also utilize the database, so a connection to MySQL is necessary.
After the PHP function is done, the output is then relayed back to the web server in HTML format. Also, note that sometimes a new entry in the database is made. The Apache web server then serves the dynamic content to the browser.
How To Set Up A LAMP Stack On AWS
Step 1: Launch An EC2 Instance
The following steps are taken to launch an EC2 Instance on AWS:
- On the EC2 Dashboard, click on the Launch Instance button.
- Give the EC2 Instance a name of your choice and search for the preferred Virtual Server (i.e. Ubuntu).
- Select the Amazon Machine Image. Ubuntu server 22.04 was selected.
- Create a key pair login, the key pair login was used to SSH into the Instance.
- Give the key pair a name, select the
.pem
format and a private key will be sent to the Downloads folder on your computer.
- Create a security group and allow SSH traffic from any IP address as shown below.
- Click on the Launch Instance button.
Step 2: Connect To EC2 Using SSH
The following steps are taken to SSH into an EC2 instance:
- On the EC2 Dashboard, click on the Running Instances tab.
- Click on the Instance ID of the Running Instance.
- Click on the Connect button of the Instance ID summary.
- The highlighted commands in the image below are used to SSH into the EC2 Instance.
On your terminal, run the following command
cd Downloads
to go to the location of the.pem
private key file.Run the code shown below to change file permissions for the
.pem
private key file:
sudo chmod 0400 <private-key-name>.pem
- Finally, connect to the EC2 Instance by running the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>
Step 3: Installing Apache
- Update the list of packages in the package manager.
sudo apt update
- Run apache2 package installation.
sudo apt install apache2
- Run the systemctl status command to check if apache2 is running, if it is green then apache2 is running correctly. Your first web server has been launched.
sudo systemctl status apache2
Step 4: Updating The Firewall
Before any traffic can be received by the web server, you need to open TCP port 80 which is the default port browsers use to connect to access web pages on the internet. The following steps are taken to open TCP port 80:
Click on Security Groups on the EC2 Dashboard.
Click on the default security group ID.
- Click on Edit Inbound Rules.
- Add a rule that allows HTTP (port 80) and all IPv4 addresses to connect.
Finally, the server can now be accessed locally and from any IPv4 address. To check if you can access the server locally in Ubuntu, run the following command:
curl http://localhost:80
To check if the apache HTTP server can respond to requests from the Internet, open your browser and run the following URL:
http://<Public-IP-Address>:80
The public IP address can be retrieved by running the following command:
curl -s http://169.254.169.254/latest/meta-data/public-ipv4
It can also be retrieved by clicking on the Instance ID of the Running Instance as shown below:
Step 5: Installing MySQL
The following steps are taken to install MySQL:
- Install the MySQL package using apt.
sudo apt install mysql-server
- Log into the MySQL console by running the command below:
sudo mysql
This will connect to the MySQL server as the administrative database user root. You should see an output like this:
- Run a security script that comes pre-installed with MySQL. This script removes insecure default settings and locks down access to your database system. Before running the script, you will set a password for the root user, using mysql_native_password as the default authentication method. You are defining this user's password as
PassWord.1
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';
- Exit the MySQL shell by running the command below:
- Start the interactive script by running the command below:
sudo mysql_secure_installation
- This will ask if you want to configure the
VALIDATE PASSWORD PLUGIN
. Enabling this feature is something of a judgment call. If enabled, passwords that don't match the specified criteria will be rejected by MySQL with an error. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials. AnswerY
for yes for the validate password plugin prompt.
- If you select "yes", you will be asked to select the level of password validation. Keep in mind that if you enter
2
for the strongest level, your password will need to contain a numeric, mixed case and special character e.g.PassWord.1
. Note the highlighted password has already been set so you selectn
to leave the password for root unchanged.
- Remove anonymous users by typing
y
.
- Disallow root login remotely by typing
n
.
- Disallow removing test database and access by typing
n
and Reload privilege tables by typingy
.
- Test if you can log into the MySQL console by running the command below:
sudo mysql -p
The -p
flag in the command will prompt you for the password.
- Exit the MySQL console by typing
exit
.
Step 6: Installing PHP
In addition to the PHP
package, you'll need php-mysql
, a PHP module that allows PHP to communicate with MySql-based databases and you'll also need libapache2-mod-php
to enable apache to handle PHP files. Core php packages will automatically be installed as dependencies.
- To install these 3 packages at once, run the following command:
sudo apt install php libapache2-mod-php php-mysql
- Once the installation has been completed, run the command below to check the version of PHP installed.
php -v
At this point, the LAMP stack is completely installed and fully operational.
Step 6: Enable PHP On The Website
Within the default Directory Index settings on Apache, a file named index.html
will always take precedence over an index.php
file. If you run the command sudo vim /etc/apache2/mods-enabled/dir.conf
, it will display a prompt of the order of preference of files in the Directory Index. The order of preference is from left to right.
sudo vim /etc/apache2/mods-enabled/dir.conf
- Hence, to prioritize the
index.php
file, move its position as shown below.
- After saving and closing the file, reload apache for changes to take effect.
sudo systemctl reload apache2
- Create a PHP script to test if PHP is correctly installed on your server. Run the following command and write the code below into the empty file you created as shown below:
sudo vim /var/www/projectlamp/index.php
- The default directory the apache server will search for files is
/var/www/html
and the server not be able to load theindex.php
file on your browser since the php file isn't in that directory. To change the directory to/var/www/projectlamp
, run the following command:
sudo vim /etc/apache2/sites-available/000-default.conf
- When you are finished, refresh your browser and you'll get a page similar to this:
After checking the relevant information about your PHP server through that page, it's best to remove the file you created as it contains sensitive information about your PHP environment and Ubuntu server. Remove the file using the command below:
sudo rm /var/www/projectlamp/index.php
Step 7: Creating A Virtual Host For Your Website Using Apache
- Assign ownership of the directory to the
$USER
environment variable which references your current system user:
sudo chown -R $USER:$USER /var/www/projectlamp
- Then create and open a new configuration file in apache's
sites-available
directory using the command below:
sudo vi /etc/apache2/sites-available/projectlamp.conf
- Copy the following code into the
projectlamp.conf
file and save:
<VirtualHost *:80>
ServerName projectlamp
ServerAlias www.projectlamp
ServerAdmin webmaster@localhost
DocumentRoot /var/www/projectlamp
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- Use the
ls
command to show the new file in the sites-available directory as shown below:
sudo ls /etc/apache2/sites-available
- Use the a2ensite command to enable the new virtual host:
sudo a2ensite projectlamp
- You might want to disable the default website that comes with apache. This is required if you're not using a custom domain name because in this case, apache's default configuration would overwrite your virtual host. To disable apache's default website, use the a2dissite command.
sudo a2dissite 000-default
- To make sure your configuration file doesn't contain syntax errors, run the command below:
sudo apache2ctl configtest
- Finally, reload apache so these changes can take effect.
sudo systemctl reload apache2
- Your website is now active but the web root
/var/www/projectlamp
is empty. Create anindex.html
file in that location so you can test if the virtual host works as expected using the command below:
touch /var/www/projectlamp/index.html
- Run the command below to input information into the
index.html
file so you can test if the virtual host works as well.
sudo echo 'Hello LAMP from hostname' $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) 'with public IP' $(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) > /var/www/projectlamp/index.html
- Go to your browser and open your website URL using the IP address:
http://<Public-IP-Address>:80
If you see the text from the echo
command you wrote to the index.html
file, then it means your apache virtual host is working as expected.