How to Install Apache, MySQL, and PHP (LAMP) on Ubuntu

The LAMP stack is a popular open-source web platform that uses Linux as the operating system, Apache as the web server, MySQL (or MariaDB) as the database management system, and PHP as the scripting language. 

A user with sudo privileges.


How to Install Apache on Ubuntu

1. Update the Package Repository
Before installing any software, it is always a good practice to update the package repository to ensure you have the latest software versions and security updates.

# sudo apt update
# sudo apt upgrade -y

2. Install Apache

# sudo apt install apache2 -y

Ensure Apache is running and to enable it to start at boot

# sudo systemctl start apache2
# sudo systemctl enable apache2
# sudo systemctl status apache2


How to Install MySQL on Ubuntu

1. Install MySQL

# sudo apt install mysql-server -y

2. Secure MySQL Installation

MySQL includes a security script that helps to improve the security of your MySQL installation by setting the root password, removing anonymous users, disallowing root login remotely, removing test databases, and reloading the privilege tables.

# sudo mysql_secure_installation

3. Verify MySQL Installation

To check if MySQL is running, use the following command:

# sudo systemctl status mysql


How to Install PHP (LAMP) on Ubuntu

Apache and mySQL are the web server component of the LAMP stack. So as per above Install Apache and MySQL, start and Enable it.

1. Install PHP

PHP is the programming language component of the LAMP stack. Install PHP along with common PHP modules:

# sudo apt install php libapache2-mod-php php-mysql -y

To check the PHP version installed, use:

# php -v

2. Configure Apache to Use PHP

By default, Apache serves the index.html file. We need to configure Apache to prioritize PHP files.

# sudo nano /etc/apache2/mods-enabled/dir.conf

Move index.php to the first position:


    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm


Save the file and exit the editor.


3. Restart Apache to apply the changes:

# sudo systemctl restart apache2


4. Test PHP

To test PHP, create a new file named info.php in the web root directory:

# sudo nano /var/www/html/info.php

Add the following PHP code to the file:


Now, open your web browser and visit:  http://your_server_ip/info.php


5. Remove the Test File

It’s a good security practice to remove the info.php file after verifying your PHP installation, as it contains sensitive information about your server configuration.

# sudo rm /var/www/html/info.php


This is how you can Install and configure the LAMP stack on your Ubuntu server. Your server is now ready to host dynamic websites and web applications using Apache, MySQL, and PHP.


Log Files for Troubleshooting: 


Apache Log Files: Apache keeps its log files in the /var/log/apache2/ directory.

1. Access Log: Contains a record of all requests received by the server.

Location: /var/log/apache2/access.log

2. Error Log: Contains diagnostic information and errors encountered by the server.

Location: /var/log/apache2/error.log


MySQL Log Files: MySQL keeps its log files in the  /var/log/mysql/  directory.

1. Error Log: Contains information about errors that occur while the server is running.

Location: /var/log/mysql/error.log (For MySQL 5.7 and older)

Location: /var/log/mysql/error.log or /var/log/mysql/mysql-error.log (For MySQL 8.0 and newer)


2. General Query Log: Logs established client connections and executed queries. This is usually disabled by default.

Location: Defined in the MySQL configuration file /etc/mysql/mysql.conf.d/mysqld.cnf (look for general_log_file).

3. Slow Query Log: Logs queries that exceed a certain execution time. This is also usually disabled by default.

Location: Defined in the MySQL configuration file /etc/mysql/mysql.conf.d/mysqld.cnf (look for slow_query_log_file).

Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 64