LAMP Installation
Apache, MySQL, and PHP Installation Guide
Welcome to the Apache, MySQL, and PHP (LAMP) Installation Guide! This tutorial will guide you through the process of installing and setting up Apache, MySQL, and PHP on your Ubuntu server.
Step 1: Installing Apache and Updating the Firewall
Before you begin, ensure that your package cache is up-to-date:
1
sudo apt update
If this is the first time you’re using the sudo
command, it will prompt you for your password to gain necessary permissions.
Once the update completes, you can install Apache with:
1
sudo apt install apache2
After executing this command, apt
will show the packages to be installed and the required disk space. Press Y
and hit enter
to confirm.
Adjust the Firewall to Allow Web Traffic
If you have enabled the UFW firewall, ensure that it allows HTTP and HTTPS traffic. Check if UFW has an application profile for Apache by running:
1
sudo ufw app list
You should see an output similar to this:
1
2
3
4
5
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
Apache Full
allows traffic on ports 80
and 443
. To get more details about this profile, use:
1
sudo ufw app info "Apache Full"
This should display:
1
2
3
4
5
Profile: Apache Full
Title: Web server (HTTP, HTTPS)
Description: Apache v2 is the next generation of the Apache web server.
Ports:
80, 443/tcp
To permit incoming HTTP and HTTPS traffic, execute:
1
sudo ufw allow "Apache Full"
Verify your setup by visiting your server’s public IP address in a web browser. You should see the Apache welcome page, indicating a successful installation.
Finding Your Server’s IP Address
If you’re unsure of your server’s public IP address, you can discover it with:
1
ip a
Step 2: Installing MySQL
With Apache up and running, the next step is to install MySQL, a popular Database Management System (DBMS). Use apt
to install the MySQL server:
1
sudo apt install mysql-server
Note: It’s not necessary to run sudo apt update
again before this command. This is optional for security.
After installation, run the following command to secure your MySQL installation:
1
sudo mysql_secure_installation
You’ll be prompted to configure the VALIDATE PASSWORD PLUGIN
. Type Y
to enable it:
1
2
3
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of passwords.
Press y|Y for Yes, any other key for No: y
To test if you can log into the MySQL console, use:
1
sudo mysql
This command connects to the MySQL server as the administrative user root
, thanks to sudo
. The output should look like this:
1
2
3
4
5
6
7
8
9
10
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.34-0ubuntu0.18.04.1 (Ubuntu)
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Create a new user as detailed in [MySQL - User Management]. Exit the MySQL console with:
1
mysql> exit
Step 3: Installing PHP
Next, install PHP along with the necessary modules to integrate it with Apache and MySQL. Run the following command to install PHP, the Apache PHP module, and the MySQL PHP module:
1
sudo apt install php libapache2-mod-php php-mysql
This should complete the PHP installation without issues. If you encounter any errors, you might need to install additional packages like php-xml
:
1
sudo apt install php-xml
Step 4: Setting Up a Virtual Host
By default, Apache serves files from the /var/www/html
directory. To create a new project, first create a new directory:
1
sudo mkdir /var/www/project
Assign ownership of this directory to the current user:
1
sudo chown -R $USER:$USER /var/www/project
Ensure the directory permissions are correct:
1
sudo chmod -R 755 /var/www/project
Create a sample index.html
page to verify the server’s functionality:
1
sudo nano /var/www/project/index.html
Add the following HTML content:
1
2
3
4
5
6
7
8
<html>
<head>
<title>Welcome to project!</title>
</head>
<body>
<h1>Success! The project server block is working!</h1>
</body>
</html>
Save and close the file.
Next, create a new Apache configuration file for your project:
1
sudo gedit /etc/apache2/sites-available/project.conf
Add the following configuration block:
1
2
3
4
5
<VirtualHost *:80>
DocumentRoot /var/www/project
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file.
Enable the new site configuration with a2ensite
:
1
sudo a2ensite project.conf
Disable the default site configuration:
1
sudo a2dissite 000-default.conf
Check for any configuration errors:
1
sudo apache2ctl configtest
The output should be:
1
Syntax OK
Restart Apache to apply the changes:
1
sudo systemctl restart apache2
Verify that Apache is serving your site by visiting http://localhost/project
in your browser. You should see the index.html
page you created.
Step 5: Testing PHP
To test PHP integration, create a PHP script called info.php
:
1
sudo gedit /var/www/project/info.php
Add the following PHP code:
1
2
3
<?php
phpinfo();
?>
Save and close the file.
Visit http://localhost/project/info.php
in your browser. You should see a PHP information page displaying various details about your PHP installation.
For security purposes, remove the info.php
file after testing:
1
sudo rm /var/www/project/info.php
You can recreate this page whenever you need to check PHP details.
This completes the Apache, MySQL, and PHP installation and configuration guide. If you encounter any issues or need further assistance, feel free to ask for help.