# Set up LAMP (Linux, Apache, MySQL, PHP) in Ubuntu Linux

**LAMP: A Comprehensive Open Source Web Development Platform**

**LAMP**, an acronym for Linux, Apache, MySQL, and PHP,  serves as a robust open-source framework for web development. Utilizing Linux as the operating system, Apache as the web server, MySQL as the relational database management system, and PHP.  LAMP is the go-to choice for creating dynamic websites and web applications. Its popularity stems from its flexibility, making it a staple in the web development community.

## **Prerequisites**&#x20;

It works on all Linux distributions.

* Operating system used: [Ubuntu 22.04](https://docs.neevcloud.com/neevcloud-products/computes/getting-started-launch-vms)
* [Install Apache](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/how-to-install-apache-on-ubuntu-22.04)
* [Install MySQL](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/how-to-install-mysql-on-ubuntu-22.04)
* [Install PHP](https://docs.neevcloud.com/neevcloud-guide/neevcloud-knowledgebase/set-up-lamp-linux-apache-mysql-php-in-ubuntu-linux#installing-and-testing-php-processing)

## Update && Upgrade the server

Installing Apache and Updating the Firewall

To update and upgrade the package lists from the repositories, run the following command:

```
sudo apt update -y 
sudo apt upgrade -y
```

Install Apache&#x20;

```
sudo apt install apache2 -y
```

Once the installation is complete, please modify your firewall settings as necessary.

```
sudo ufw app list
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FTknJlY6IW2qEC6RE9Nj7%2Fimage.png?alt=media&#x26;token=9cf4ff24-b772-44f6-9585-a83a3f4b108d" alt=""><figcaption></figcaption></figure>

Here is what each of these profiles means:

Apache: This profile opens only port **80** (normal, unencrypted web traffic).

Apache Full: This profile opens both port 80 and port 443

Apache secur&#x65;*:* This profile opens only port 443 (TLS/SSL encrypted traffic.)

To only allow traffic on port 80, use the Apache profile:

```
sudo ufw allow in "apache"
sudo ufw status 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FTinWCWTgcrDUKr9wKPN5%2Fimage.png?alt=media&#x26;token=2d788b48-57ce-4745-ba87-10eb5723df0d" alt=""><figcaption></figcaption></figure>

**Login to browser -**[ **http://your\_server\_IP** ](http://server_domain_or_ip)

The default Ubuntu Apache web page serves both informational and testing purposes.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fq2UVGo69GyQiF5MsCuyF%2Fimage.png?alt=media&#x26;token=913ec0b8-b400-465a-91c0-bb02285e62d8" alt=""><figcaption></figcaption></figure>

### **Install MySQL**&#x20;

To store and manage data for your website, you must install a database system. **MySQL** is a widely used database management system, often partnered with PHP environments.

```
sudo apt install mysql-server -y
```

When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system.

```
sudo mysql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FBWPYu2uTlKV5nvaRTT7p%2Fimage.png?alt=media&#x26;token=cc24ac48-a1df-4a18-9611-2deb0fe51680" alt=""><figcaption></figcaption></figure>

Start the interactive script by running:

```
sudo mysql_secure_installation 
```

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.

Answer Y for yes, or anything else to continue without enabling.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FES5YJf5hVc0fAPag3GzF%2Fimage.png?alt=media&#x26;token=7dfd2977-69e9-45da-9e66-3aa39bde74ef" alt=""><figcaption></figcaption></figure>

If you select "Yes," you'll need to choose a password validation level. Note that choosing the highest level `2`means your passwords must include numbers, uppercase and lowercase letters, and special characters. Otherwise, you'll encounter errors.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FnJIxHjoQbGrai5C5MEVc%2Fimage.png?alt=media&#x26;token=4f71dd6b-5db7-4a4e-9b65-044b661853e5" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FNdyMGxmwZrvs1ciJCTAH%2Fimage.png?alt=media&#x26;token=a49a9aa7-d294-4788-a0f0-3b51167adf86" alt=""><figcaption></figcaption></figure>

**Run these commands -**&#x20;

```
mysql -u root -p 
```

After executing the commands, please enter your password:

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FY6W3v0v0rewfA0nyxQ6t%2Fimage.png?alt=media&#x26;token=b2fd479e-e544-4318-8026-4d625390fa6f" alt=""><figcaption></figcaption></figure>

### Creating a MySQL Database

To create a database in **MySQL**, follow these steps:

{% hint style="info" %}
Open your terminal or command prompt. Log into MySQL with the command `mysql -u username -p`. Replace `username` with your MySQL username. You'll be prompted to enter your password.
{% endhint %}

{% hint style="info" %}
Once logged in, create a new database by executing `CREATE DATABASE mydatabase;`. Replace `mydatabase` with your desired database name.
{% endhint %}

{% hint style="info" %}
To verify the database was created, use `SHOW DATABASES;`. Your new database should appear in the list.
{% endhint %}

This process creates a new **MySQL** database ready for use.

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fy5zIdJ3p9I7ISh0j3O3m%2Fimage.png?alt=media&#x26;token=09e8d318-a771-4aad-9f48-2adc9c4261b2" alt=""><figcaption></figcaption></figure>

To create a new database, run the following command from your **MySQL** console:

```
mysql> CREATE DATABASE example_database;
```

Now give this user permission over the `example_database` databas&#x65;**:**

```
mysql> GRANT ALL PRIVILEGES ON example_database.*  TO  'root'@'%';
```

Now, review MySQL configurations.

```
sudo mysql
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F8OhRbh7YMUSWA3Pw6dfJ%2Fimage.png?alt=media&#x26;token=b8062798-93d3-444f-bfa3-03efc080aa09" alt=""><figcaption></figcaption></figure>

### **Installing and testing PHP processing**&#x20;

Our setup includes Apache as the webserver to deliver content, MySQL for data storage and management, and PHP to process and render dynamic content to users.

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

After completing the installation, execute the command below to verify your PHP version:

```
php -v
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FMTqnonMh8FAKtk09hM4j%2Fimage.png?alt=media&#x26;token=a92dd6f7-b6e7-4a49-a900-5f4c33f2436a" alt=""><figcaption></figcaption></figure>

### **Modifying Apache's DirectoryIndex Configuration**

To adjust the file Apache uses by default when serving directories, edit the `DirectoryIndex` directive in your Apache configuration file (**httpd.conf** or **apache2.conf** depending on your operating system). This change alters which file types Apache prioritizes when a directory is accessed.

```
sudo vi /etc/apache2/mods-enabled/test.conf
```

It will look like this:

#### **Add - index.php**

```
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.p1 index.xhtml index.htm
</TfModule>
```

After restarting the Apache web server, confirm the apache2 service status using the systemctl command.

```
sudo systemctl restart apache2
sudo systemctl status apache2
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FpgLvoDCkTbY7bhF9242T%2Fimage.png?alt=media&#x26;token=71791f87-a96a-4036-8e9f-3f09f409e085" alt=""><figcaption></figcaption></figure>

Now that all your services are running create a PHP test script to confirm that Apache can handle and process requests for PHP files on the path *`/var/www/<DOMAIN_NAME>/`*.

```
vi info.php
```

Add the following text, which is valid PHP code, inside the file by typing `i`

```
<?php
phpinfo();ESC
?>
```

Press ESC *`:wq`* to save

To ensure everything is set up correctly,  visit your SERVER\_PUBLIC\_IP address in your web browser for a spot check.

<pre><code><strong>http://&#x3C;SERVER_PUBLIC_IP>/info.php
</strong></code></pre>

### **Creating a Virtual Host for your Website**&#x20;

To create a directory for `your_domain`, follow these steps:

1. Open your terminal.
2. Navigate to the location where you want to create the directory.
3. Enter the command `mkdir your_domain` and press Enter.

This creates a new directory named `your_domain` in the specified location.

```
sudo mkdir -p /var/www/example.com
```

To change the ownership of the directory to your current system user, use the `$USER` environment variable like this:

```
sudo chown -R $USER:$USER /var/www/example.com
```

To create a new configuration file within Apache's **sites-available** directory, use your favorite command-line editor. We will use **vi** for this example:

```
sudo vi /etc/apache2/sites-available/example.com.conf
```

To create a new blank file, insert the following base configuration and replace `yourdomain.com` with your actual domain name:

```
<VirtialHost *:80>
    ServerName <YOUR_DOMAIN>
    ServerAlias www.<YOUR_DOMAIN>
    ServerAdmin admin@localhost
    DocumentRoot /var/www/<YOUR_DOMAIN>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```

Now, use `a2ensite`to enable the new virtual host:

```
sudo a2ensite example.com
```

To disable the Apache default website—essential when not using a custom domain, as it would override your virtual host configuration—run the following command:

```
sudo a2dissite 000-default
```

Ensure Your Configuration File is Error-Fre&#x65;**:** To verify that your configuration file is free of syntax errors, execute the command below:

```
sudo apache2ctl configtest
```

Finally, reload Apache so these changes take effect :

```
sudo systemctl reload apache2
```

*Your new website is now active, but /var/www/\<YOUR\_DOMAIN>is still empty. Create an index.html*

```
vi /var/www/example.com/index.html
```

Include the following content in the file:

```
<html>
  <head>
    <title>your_domain website</title>
  </head>
  <body>
    <h1>Hello World!</h1>

    <p>This is the landing page of <strong>YOUR_DOMAIN</strong>.</p>
  </body>
</html>
```

Save and close the file, then go to your browser and access your server's domain name or IP Address:

```
http://<SERVER_PUBLIC_IP>
```

```
http://<YOUR_DOMAIN>
```

You can leave this file in place as a temporary landing page for your application until you set up a `index.php` file to replace it. Once you do that, remember to remove or rename the `index.html` file from your document root, as it would take precedence over a `index.php` file by default.
