Step-by-Step Guide to Install a LAMP Stack on Ubuntu 22.04
What is a LAMP Stack?
LAMP stands for Linux, Apache, MySQL, and PHP. It is a popular web development stack that enables users to build dynamic websites and web applications. In this guide, you will learn how to install each component of the LAMP stack on Ubuntu 22.04.
Step 1: Update the System
Before starting, ensure your system is up-to-date. Use the following commands to update your package lists and upgrade installed packages:
sudo apt update
sudo apt upgrade -y
Step 2: Install Apache
Apache is the web server component of the LAMP stack. To install it, execute:
sudo apt install apache2 -y
To check if Apache is running, use:
sudo systemctl status apache2
You should see an active (running) status. If it’s not running, start it with:
sudo systemctl start apache2
To ensure Apache starts on boot, run:
sudo systemctl enable apache2
Step 3: Configure Apache Firewall
To allow web traffic, you may need to modify the UFW firewall settings. To allow HTTP and HTTPS traffic, use:
sudo ufw allow 'Apache Full'
You can check the status with:
sudo ufw status
Step 4: Install MySQL
Next, install the MySQL database server, which is responsible for data storage and management:
sudo apt install mysql-server -y
Secure your MySQL installation with the security script:
sudo mysql_secure_installation
Follow the prompts to set a root password and remove test databases and users.
To log into MySQL, use:
sudo mysql -u root -p
Enter your password when prompted.
Step 5: Create a Database and a User in MySQL
Once you are logged in to the MySQL prompt, create a database for your application:
CREATE DATABASE my_database;
Create a user and grant them privileges:
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace my_database, my_user, and my_password with your desired database name, username, and password.
Step 6: Install PHP
PHP is the programming language that runs on the server for dynamic web content. Install PHP and necessary libraries:
sudo apt install php libapache2-mod-php php-mysql -y
To confirm the installation, create a PHP info file:
echo "" | sudo tee /var/www/html/info.php
Navigate to http://your_server_ip/info.php in your web browser. You should see the PHP information page.
Step 7: Test Apache with PHP
To verify Apache is processing PHP files, create a test file. If you have not already created info.php, follow the earlier command. Then, access this file:
http://your_server_ip/info.php
You will find a comprehensive page with details about your PHP configuration, which confirms that PHP is successfully running under Apache.
Step 8: Install Additional PHP Extensions
Depending on your application requirements, you might need additional PHP modules. Popular extensions include:
sudo apt install php-curl php-gd php-xml php-mbstring php-zip -y
After installing any new PHP extensions, restart Apache to apply the changes:
sudo systemctl restart apache2
Step 9: Set Up Virtual Hosts (Optional)
If you plan to host multiple websites, set up virtual hosts for easy management. Create a new configuration file for your site:
sudo nano /etc/apache2/sites-available/my_site.conf
Add the following configuration:
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/my_site
AllowOverride All
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Create the directory for your site:
sudo mkdir /var/www/my_site
Enable the site and reload Apache:
sudo a2ensite my_site.conf
sudo systemctl reload apache2
Step 10: Install phpMyAdmin (Optional)
To manage your databases via a web interface, install phpMyAdmin:
sudo apt install phpmyadmin -y
During installation, select Apache when prompted and configure the database for phpMyAdmin. You will be asked to configure the database and provide an application password.
After installation, include the phpMyAdmin configuration in Apache:
echo 'Include /etc/phpmyadmin/apache.conf' | sudo tee -a /etc/apache2/apache2.conf
Step 11: Testing phpMyAdmin Access
Access phpMyAdmin in your web browser by visiting:
http://your_server_ip/phpmyadmin
Log in with the MySQL user credentials created earlier.
Step 12: Finishing Up
After confirming all components are working, ensure to delete the info.php file for security reasons:
sudo rm /var/www/html/info.php
At this point, your LAMP stack is ready for use. You can start developing your web applications on your Ubuntu 22.04 setup.
Maintain security best practices, including regular updates and backups, to ensure a smooth deployment and operation of your web applications.
Leave a Reply