Setting up a LAMP (Linux, Apache, MySQL, PHP) stack on the latest long-term release of Ubuntu involves several steps. This guide assumes you have a clean installation of Ubuntu and a user account with sudo privileges.
Step 1: Update Package Lists
sudo apt update sudo apt upgrade
Step 2: Install Apache Web Server
sudo apt install apache2
Start the Apache service:
sudo systemctl start apache2
Enable Apache to start on boot:
sudo systemctl enable apache2
Step 3: Install MySQL Server
sudo apt install mysql-server
During the installation, you'll be prompted to set a password for the MySQL root user. Follow the instructions to complete the setup.
Start the MySQL service:
sudo systemctl start mysql
Enable MySQL to start on boot:
sudo systemctl enable mysql
Step 4: Install PHP and Required Modules
sudo apt install php libapache2-mod-php php-mysql
Step 5: Configure Apache to Use PHP
Edit the Apache default configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
<VirtualHost>
block, just before the closing </VirtualHost>
tag:<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Save and exit the file.
Restart Apache to apply the changes:
sudo systemctl restart apache2
Step 6: Test PHP
Create a PHP test file in the default web root directory:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
http://your_server_ip/info.php
in a web browser. You should see a page with PHP information.Step 7: Configure MySQL
Run the MySQL secure installation script:
sudo mysql_secure_installation
Follow the on-screen prompts to set up security options for your MySQL installation.
Step 8: Create a MySQL Database and User
Login to MySQL:
sudo mysql
Run the following commands to create a new database and user, and grant privileges:
CREATE DATABASE your_database_name; CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_user'@'localhost'; FLUSH PRIVILEGES; EXIT;
your_database_name
, your_user
, and your_password
with your preferred values.Step 9: Test the LAMP Stack
Create a simple PHP script to test the connection to the MySQL database:
echo "<?php
\$conn = new mysqli('localhost', 'your_user', 'your_password', 'your_database_name');
if (\$conn->connect_error) {
die('Connection failed: ' . \$conn->connect_error);
}
echo 'Connected successfully';
?>" | sudo tee /var/www/html/dbtest.php
http://your_server_ip/dbtest.php
in a web browser. If everything is configured correctly, you should see "Connected successfully."Step 10: Firewall Configuration (Optional)
sudo ufw allow 80
Reload the firewall:
sudo ufw reload
Congratulations! You've successfully set up a LAMP stack on the latest long-term release of Ubuntu, with PHP as the backend language and MySQL as the database component. This configuration provides a solid foundation for hosting dynamic web applications.