In this ubuntu tutorial, we are going to learn how to install and setup a LAMP (Linux, Apache, MySQL, PHP) stack on Ubuntu 22 this installation involves several steps, this installation including updating the ubuntu system, installing the Apache web server, installing MySQL database, and configuring PHP programming language on Ubuntu 22. This is the full detailed step-by-step guide on how to install the LAMP stack on Ubuntu 22:
Step 1: Update the system
Before installing the LAMP stack on ubuntu 22, we have to update the ubuntu system to confirm that all the packages are up-to-date, to update the system run the below command on your terminal:
sudo apt-get update
Step 2: Install Apache
Apache is the open-source web server component of the LAMP stack. To install the Apache web server on Ubuntu 22, run the below command on your terminal:
sudo apt-get install apache2
Step 3: Install MySQL
MySQL is a relational database management system (RDBMS) that is part of the LAMP stack. To install MySQL on Ubuntu 22, run the below command on your terminal:
sudo apt-get install mysql-server
During the MySQL installation, it will prompt you to set a root password for MySQL. We have to choose a strong and secure password.
Step 4: Install PHP
PHP is the programming language component of the LAMP stack. To install PHP and its modules on your Ubuntu 22, run the below command on your terminal:
sudo apt-get install php libapache2-mod-php php-mysql
The above command will install PHP and its required modules.
Step 5: Configure Apache to use PHP
Now we have to configure Apache to use PHP, you need to edit the Apache configuration /etc/apache2/mods-enabled/dir.conf file, run the below command to open the file:
sudo nano /etc/apache2/mods-enabled/dir.conf
This file controls how Apache handles different file types, so Apache web server knows to use PHP for files with a .php extension.
Step 6: Restart Apache
After changing /etc/apache2/mods-enabled/dir.conf file, we have to restart the Apache web server for the changes to take effect. To restart Apache, run the below command:
sudo service apache2 restart
Step 7: Create a PHP test file
After LAMP installation now confirm that your LAMP stack works correctly on your ubuntu 22, you can create a simple info.php test file to test your LAMP installation. Open your terminal and run the below command:
sudo nano /var/www/html/info.php
This will create a new file. Add the following PHP code to the file:
<?php phpinfo(); ?>
Save and close the file.
Step 8: Test PHP
In your web browser, navigate to http://localhost/info.php. You should see a page displaying information about your PHP installation, including the version number and information about loaded modules.
Step 9: Create a database
To create a database, you will need to log in to the MySQL command-line interface. Run the below command to log in to your MySQL:
mysql -u root -p
It will prompt you to enter the root password for MySQL that you set during the MySQL installation.
Step 10: Create a new database
Once you are logged in to MySQL, you can create a new database by running the following command:
CREATE DATABASE databasename;
Step 11: Create a new user
To create a new MySQL user, you have to run the below command:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
Step 12: Grant privileges
To grant the new MySQL user privileges on the new database, you have to run the below command:
GRANT ALL PRIVILEGES ON databasename.* TO 'username'@'localhost';
Step 13: Flush privileges
After granting privileges, you need to flush the privileges so that the changes take effect immediately. You can use the following command to flush privileges:
Step 14: Connect to the database
To connect to the new database, you can use the following command:
Your LAMP stack is now set up and ready to use. You can now start building dynamic web pages and web applications using Linux, Apache, MySQL, and PHP.
In conclusion, installing a LAMP stack on Ubuntu 22 is a simple process that allows you to set up a powerful web development environment. By following the steps outlined in this guide, you can successfully install Linux, Apache, MySQL, and PHP on your machine, and start building dynamic web pages and web applications.