Setting Up a LAMP Stack on Ubuntu: A Step-by-Step Guide

20 Oct 2024 02:44 AM

Setting up a LAMP stack (Linux, Apache, MySQL, PHP) on Ubuntu is a common practice for building and testing web applications. The stack provides a robust platform to host websites or applications and includes four key components: Linux as the operating system, Apache as the web server, MySQL for database management, and PHP to process dynamic content. In this tutorial, we'll walk through the steps to set up a LAMP stack on Ubuntu.

Prerequisites

  • A fresh install of Ubuntu (preferably the latest version).
  • Root or sudo privileges on the server.
  • Basic knowledge of the Linux terminal.

Let’s get started!


Step 1: Update Package Repository

The first thing you want to do is ensure that your server is up-to-date by running:

sudo apt update && sudo apt upgrade

This will update the package lists and install any available updates.


Step 2: Install Apache Web Server

Apache is a widely-used open-source web server. To install Apache on Ubuntu, run the following command:

sudo apt install apache2

Once the installation is complete, you can start Apache and enable it to start on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Now, verify that Apache is working by visiting your server’s IP address in a web browser:

http://your-server-ip

If the installation was successful, you should see the default Apache welcome page.


Step 3: Install MySQL (Database Server)

Next, we’ll install MySQL to manage our databases. Install MySQL by typing:

sudo apt install mysql-server

After the installation, secure the MySQL installation with:

sudo mysql_secure_installation

This script will prompt you to configure security options, such as setting a root password, removing anonymous users, disallowing remote root logins, and so on.

Now, check if MySQL is running:

sudo systemctl status mysql

You can start and enable MySQL on boot with:

sudo systemctl start mysql
sudo systemctl enable mysql

To enter the MySQL shell and perform administrative tasks, use:

sudo mysql

Step 4: Install PHP (Server-Side Scripting Language)

PHP is the language used for dynamic content processing. To install PHP and its Apache integration, run:

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

To check that PHP was installed successfully, create a PHP info file:

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

Add the following content to the file:

<?php
phpinfo();
?>

Now, save and exit the file. You can view the output by visiting:

http://your-server-ip/info.php

If PHP is working correctly, you’ll see a detailed PHP info page.


Step 5: Adjust Apache Configuration to Prioritize PHP

By default, Apache serves index.html before index.php files. To change this, edit Apache’s dir.conf file:

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

Look for this line:

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

Change it to:

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

Save and close the file, then restart Apache for the changes to take effect:

sudo systemctl restart apache2

Step 6: Test PHP with Apache

You should already have a info.php file in /var/www/html. Go to your browser and open:

http://your-server-ip/info.php

This should display a PHP information page showing all the details about your PHP configuration.


Step 7: Set Permissions for the Web Directory

The default web directory is /var/www/html/. To ensure proper permissions, use the following commands:

  1. Change ownership of the web directory
sudo chown -R $USER:$USER /var/www/html
  1. Set the correct permissions:
sudo chmod -R 755 /var/www/html

Now you can easily add, modify, and delete files within this directory.


Step 8: Manage MySQL Databases

You can manage MySQL databases either through the command line or by using a web interface like phpMyAdmin. To manage MySQL via command line, log into the MySQL shell:

sudo mysql -u root -p

To create a database, run:

CREATE DATABASE example_database;

To create a new user and grant them permissions:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON example_database.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 9: Enable Firewall (Optional)

For added security, it’s a good idea to enable the UFW (Uncomplicated Firewall) and allow traffic only on specific ports. Allow Apache through the firewall:

sudo ufw allow 'Apache'

Then enable UFW:

sudo ufw enable

Conclusion

Congratulations! You now have a fully functioning LAMP stack on your Ubuntu server. You’ve successfully installed and configured Apache, MySQL, and PHP. From here, you can start deploying websites or web applications, building databases, and more.

Be sure to keep your server updated and secure, and explore other tools such as phpMyAdmin for easier MySQL management, and Let’s Encrypt for SSL certificates if you're moving your site to production.

Happy coding!









 

 

 

 

1
15