How to Set Up a LAMP Stack on Ubuntu 22.04 (Step-by-Step Guide)

Setting up a LAMP stack on Ubuntu 22.04 allows you to create a powerful web server environment for hosting websites and applications. LAMP stands for Linux, Apache, MySQL, and PHP, and it’s a widely used stack for web development.

In this guide, you’ll learn how to install and configure a LAMP stack on Ubuntu 22.04.

Prerequisites

Before starting, ensure you have:

  • A server or VPS running Ubuntu 22.04
  • A user with sudo privileges
  • An active internet connection

Step 1: Update System Packages

Start by updating your system packages to ensure you’re using the latest versions.

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache Web Server

Apache is the most popular web server software. Install it with the following command:

sudo apt install apache2 -y

Enable and Start Apache

sudo systemctl enable apache2
sudo systemctl start apache2

Verify Apache Installation

To check if Apache is running, type:

systemctl status apache2

You can also visit your server’s IP address in a browser:

http://your-server-ip

You should see the Apache default page.

Step 3: Install MySQL Database Server

MySQL is a relational database management system that stores website data.

sudo apt install mysql-server -y

Secure MySQL Installation

Run the security script to remove insecure default settings:

sudo mysql_secure_installation

You’ll be asked to set a root password and remove test databases. Follow the prompts and choose secure options.

Verify MySQL Status

systemctl status mysql

Test MySQL Database Connection

To test the connection to MySQL, log in as root:

sudo mysql -u root -p

Enter your root password when prompted. Then, create a test database:

CREATE DATABASE test_db;
USE test_db;
CREATE TABLE test_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100));
INSERT INTO test_table (name) VALUES ('Test Entry');
SELECT * FROM test_table;

If you see the inserted entry, your MySQL setup is working correctly. Exit MySQL by typing:

EXIT;

Create a MySQL User and Grant Privileges

For better security, create a separate MySQL user with privileges for a specific database:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON test_db.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

This user can now access and manage test_db without using the root account.

Step 4: Install PHP

PHP is a server-side scripting language used to generate dynamic web pages.

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

Install Essential PHP Extensions

To ensure compatibility with various applications, install the following PHP extensions:

sudo apt install -y php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}

Check PHP Version

php -v

Step 5: Configure Apache to Prioritize PHP Files

By default, Apache serves index.html before index.php. Modify Apache’s configuration to prioritize PHP files.

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

Move index.php to the beginning of the list:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

Save the file and restart Apache:

sudo systemctl restart apache2

Step 6: Enable Apache Site Configuration

To enable a site configuration in Apache, you need to create a new virtual host file. Run:

sudo nano /etc/apache2/sites-available/mywebsite.conf

Add the following configuration:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the file and enable the site:

sudo a2ensite mywebsite.conf

Then restart Apache:

sudo systemctl restart apache2

Step 7: Enable Apache Rewrite Module and Modify Configuration

You might have to edit /etc/apache2/apache2.conf to enable .htaccess overrides.

Open the configuration file:

sudo nano /etc/apache2/apache2.conf

Find the following section:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Change AllowOverride None to AllowOverride All:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Save and exit the file, then restart Apache:

sudo service apache2 restart

Enable the mod_rewrite module:

sudo a2enmod rewrite

Restart Apache again to apply changes:

sudo systemctl restart apache2

Step 8: Test PHP Processing

Create a PHP test file:

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

Add the following content:

<?php
phpinfo();
?>

Save and close the file, then visit:

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

If PHP is installed correctly, you will see the PHP info page.

Step 9: Install phpMyAdmin

phpMyAdmin is a web-based interface for managing MySQL databases.

sudo apt install phpmyadmin -y

During the installation, select Apache as the web server and choose Yes when asked to configure a database for phpMyAdmin.

After installation, access phpMyAdmin by visiting:

http://your-server-ip/phpmyadmin

Log in using the MySQL credentials you set up earlier.

Conclusion

You have successfully set up a LAMP stack on Ubuntu 22.04. You can now deploy PHP applications and manage MySQL databases. To further optimize performance, consider configuring SSL certificates and additional security measures.

For more tutorials on web hosting and server management, stay tuned!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Need Help ? Call Our award-winning support team 24/7 at 01-4983900

© 2023 All right reserved to sobiztrend.com