LAMP is an open-source application stack used to develop and serve web applications on the internet. The abbreviation “LAMP” stands for Linux Operating System, Apache Web Server, MySQL Database, and PHP programming Language.
This guide covers every step to installing a LAMP stack on an Ubuntu 22.04 server. The first criteria: the Linux Operating system, is met by the Ubuntu. We’ll go over how to set up and execute the remaining components.
Post Contents
Install Apache Web Server
Apache is a contemporary and efficient web server used to show web pages to our site visitors.
In a fresh Ubuntu Installation, before installing any application from the Ubuntu’s official repository, we’ll need to refresh the package manager’s DB. Ubuntu uses apt
as their package manager so we’ll need to run the following command:
sudo apt-get update
Now we can install apache web server by running:
sudo apt install apache2
apt will install and configure apache to serve web page out of the box. You can check by visiting your server IP address. If it is successfully installed, you’ll see a apache landing page like this:
Note: If your server is running behind the NAT such as Google Cloud, you need to add a firewall entry to allow port 80 and 443 to access the server’s public IP address from your browser.
Install MySQL Database Server
We have our web server running and now we need a database server to store and manage the database for running web applications on our server.
We can install MySQL Server by:
sudo apt install mysql-server
MySQL database server is installed but we need to configure it before start using. To initiate the configuration enter the following command:
mysql_secure_installation
The post configuration prompt will ask you to set root password, then VALIDATE PASSWORD PLUGIN
, remove anonymous users, test database and access to it and disallow remote root logins.
I always choose to disable validate password plugin because I can make a strong, unique password for database credentials.
A strong, unique password should have a combination of mixed case letters, numbers and special characters. Although, MySQL does not support all the special characters available. Here are the ones you should avoid:
” ‘ $ , [ ] * ? { } ~ # % \ < > | ^ ;
Here are the ones usually safe to use:
: @ . , / + – ! =
For the rest of the questions, you can press Y, it will remove anonymous users, test database, remote root login, and reload privileges.
By default, MySQL uses Unix socket authentication, which is why it doesn’t require a password for login to the MySQL shell. You might want to use a password instead. To do so, we’ll log in to the MySQL shell and then create a new user with the following command:
sudo mysql
you’ll see a mysql> prompt, to create a new user with MySQL native authentication plugin which is required by most of the PHP applications, run the following command in the MySQL shell prompt:
CREATE USER 'admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Now we need to grant all privileges to the admin
user.
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
Finally, the new privileges should be enforced by:
FLUSH PRIVILEGES;
Install PHP for processing dynamic web pages.
We’ve installed nginx to serve web pages and MySQL to store information. Now we need to install PHP for processing dynamic web content.
sudo apt install php libapache2-mod-php php-mysql
There are some optional PHP extensions often required by many applications or content management system such as WordPress. You can install them by running:
sudo apt install php-curl php-xml php-common php-imagick php-mbstring php-zip
Configure Apache to serve dynamic web pages
We’ve installed all the necessary components to our server now we’ll need to configure apache. The default apache configuration is located at /etc/apache2/sites-available/000-default.conf
. We’ll use nano
to edit the default configuration file.
sudo nano /etc/apache2/sites-available/000-default.conf
Then we’ll make a necessary adjustment like the config below.
<VirtualHost *:80>
ServerName example.com
ServerAlias example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Now we need to enable the configuration,
sudo a2ensite 000-default
We can now reload apache web server to changes take effect.
sudo systemctl reload apache2
We’re done installing LAMP stack on our Ubuntu 22.04 server. To verify everything works properly, we’ll create a php.info
file using nano
editor.
sudo nano /var/www/html/info.php
A blank file with nano editor will be opened, paste the following code:
<?php
phpinfo();
Save the file by pressing Ctrl + X, followed by Y , and then Enter. Now we can visit our server’s public IP address to see the results:
http://127.0.0.1/info.php
You’ll see a web page generated by PHP with your server information like this:
If you see a web page like this, Congratulations! You’ve successfully installed and configured the LAMP stack on your Ubuntu 22.04 server.
We can remove the test php.info file for now:
sudo rm /var/www/html/info.php
Conclusion
By following this tutorial you’ve installed and configured a very adaptable platform for serving web content to your visitors. The LAMP stack is now ready to deploy PHP web applications on production environment.