This article provides a step-by-step guide to installing the LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu 22.04 or 24.04. By following these instructions, you will set up a web server capable of hosting dynamic websites and applications.
Prerequisites
- A fresh installation of Ubuntu 22.04 or 24.04.
- Sudo privileges on your system.
- A non-root user account with sudo access.
Step 1: Update the System
Before installing any new packages, it's a good practice to update and upgrade the existing software on your Ubuntu server.
- Open a terminal window.
- Run the following commands:
sudo apt update
sudo apt upgrade -y
Step 2: Install Apache Web Server
Apache is one of the most popular web servers in the world. You can install it using the following command.
- In your terminal, run:
sudo apt install apache2 -y
To ensure that Apache is running and enabled to start on boot, use:
sudo systemctl status apache2
sudo systemctl enable apache2
Step 3: Install MySQL Database Server
MySQL is a widely used relational database management system. Install it with the following command.
- In your terminal, run:
sudo apt install mysql-server -y
After installation, secure MySQL by running:
sudo mysql_secure_installation
Follow the prompts to set a root password and make other security-related changes.
Step 4: Install PHP
PHP is a server-side scripting language that allows you to create dynamic web pages. Install it along with some common PHP modules:
- In your terminal, run:
sudo apt install php libapache2-mod-php php-mysql -y
Step 5: Configure Apache to Process PHP Files
By default, Apache prioritizes HTML files over PHP files. To change this behavior, you need to edit the dir.conf file.
- Edit the dir.conf file:
sudo nano /etc/apache2/mods-enabled/dir.conf
Change the order of index.php and index.html so that it looks like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save the file and exit. Then, restart Apache to apply the changes:
sudo systemctl restart apache2
Step 6: Set Up a Virtual Host (Optional)
To host multiple websites on your server, you can set up virtual hosts.
- Create the directory for your website:
- Create and edit the configuration file for your site:
- Enable the new site configuration:
- Disable the default site to avoid conflicts:
- Restart Apache to apply changes:
sudo mkdir /var/www/your_domain
sudo nano /etc/apache2/sites-available/your_domain.conf
Add the following configuration, replacing your_domain with your actual domain name or IP address:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite your_domain.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
Step 7: Test PHP Processing on Apache
Create a PHP file to test if Apache can process PHP files.
- Create the info.php file:
- Add the following code to the file:
- Save and close the file.
- Visit http://your_server_ip/info.php in your web browser. You should see a page with PHP version and configuration information.
sudo nano /var/www/your_domain/info.php
<?php
phpinfo();
?>
Step 8: Configure Firewall Rules
If you have UFW (Uncomplicated Firewall) enabled, allow HTTP and HTTPS traffic.
- Allow 'Apache Full' profile:
sudo ufw allow in "Apache Full"
Troubleshooting
- Apache not starting: Check the Apache error log with
sudo tail -f /var/log/apache2/error.log. - PHP info page not displaying: Ensure that PHP is installed correctly and that the file has no syntax errors.
- Firewall issues: Verify that UFW rules are set correctly with
sudo ufw status verbose.