Setting Up a LAMP Stack on Ubuntu

Mar 8, 2023 - 09:48
Apr 8, 2023 - 10:28
 0  83
Setting Up a LAMP Stack on Ubuntu

The LAMP stack is a popular open-source software suite used for web development. It stands for Linux, Apache, MySQL, and PHP. In this guide, we'll walk through the steps to set up a LAMP stack on an Ubuntu server.

Step 1: Update Package Index

Before installing any packages, update the package index to ensure you have the latest package listings:

sudo apt update

Step 2: Install Apache

Apache is a widely used web server. Install it with the following command:

sudo apt install apache2

After the installation, enable Apache to start at boot and start the Apache service:

sudo systemctl enable apache2
sudo systemctl start apache2

To verify that Apache is running, open a web browser and navigate to your server's IP address. You should see the default Apache welcome page.

Step 3: Install MySQL

MySQL is a powerful relational database management system. Install it using:

sudo apt install mysql-server

After the installation, run the security script to enhance the security of your MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set up your root password and secure your MySQL instance.

Step 4: Install PHP

PHP is a server-side scripting language designed for web development. Install PHP along with some common PHP modules:

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

Step 5: Test PHP Processing on Your Web Server

To test PHP, create a simple PHP file in the web root directory:

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

Add the following PHP code to the file:

Save the file and exit the editor. Now, open a web browser and navigate to `http://your_server_ip/info.php`. You should see a page displaying PHP information.

Step 6: Configure Apache to Prefer PHP Files

By default, Apache serves HTML files before PHP files. To configure Apache to prefer PHP files, edit the `dir.conf` file:

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

Move the `index.php` file to the first position after the `DirectoryIndex` specification, like this:


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

Save the file and restart Apache to apply the changes:

sudo systemctl restart apache2

Step 7: Adjust Firewall Settings

If you have a firewall running, ensure that Apache is allowed to communicate through it. Allow OpenSSH and Apache Full:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable

Conclusion

You now have a fully functional LAMP stack on your Ubuntu server. This setup forms a solid foundation for developing and deploying web applications. Remember to remove the `info.php` file after testing to avoid exposing sensitive information:

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

By following these steps, you've installed and configured Apache, MySQL, and PHP on your Ubuntu server, creating a robust environment for your web development projects.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow