# PHP Site Hosting on Ubuntu

## **What Is LAMP, and How Does It Work?**

The best way to create a local web server is to install LAMP, one of the most popular stacks for building and deploying dynamic web applications. The LAMP stack uses **Linux**, **Apache**, **MySQL**, and **PHP** as its foundation.

Below is a brief explanation of how LAMP works:

1. Requests will be pointed to the Apache web server whenever a user visits your website.
    
2. The web server will look for the requested web page file and pass the information to PHP. PHP interprets and pulls the necessary data from the MySQL database to render the web content.
    
3. Finally, the Apache web server delivers the web content and displays it on the user’s web browser.
    

Now Let's see how to achieve that.

## **Add a new  user for the site**

Here I am adding the Ubuntu user

```plaintext
sudo adduser ubuntu
```

switch user and create a folder public\_html

```plaintext
su - ubuntu
mkdir public_html 
```

we will use this public\_html to store site files

Once created exit to switch back to the root user

```plaintext
exit
```

## **Update Packages in Ubuntu**

Update Packages in Ubuntu.

Add PHP repository, and install php8. ( Replace version 8 with the needed version)

Add PHP-fpm if needed.

```plaintext
sudo apt update
sudo add-apt-repository ppa:ondrej/php
sudo apt install php8.0
sudo apt install php8.0-cli php8.0-common php8.0-imap php8.0-redis php8.0-xml php8.0-zip php8.0-mbstring
sudo apt install php8.0-fpm
```

If you need additional packages. You need to install it separately.

```plaintext
sudo apt install php8.0-gd
```

## **Install Apache2**

Install apache2 webserver

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

Go to the Apache  
  
site directory and add the site data symlink

```plaintext
cd /var/www/html
```

now create a symlink

```plaintext
ln -s /home/ubuntu/public_html /var/www/html/site1
```

switch user and add data file

```plaintext
su - ubuntu && cd public_html
```

For now, we are adding phpinfo file

```plaintext
vim phpinfo.php
```

add the following data

```plaintext
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
```

save it.

switch back to the user's root

```plaintext
exit
```

Edit the configuration of Apache in sites-available

```plaintext
vim /etc/apache2/sites-available/000-default.conf
```

Paste the following

```plaintext
<VirtualHost *:80>
               ServerAdmin webmaster@localhost
               ServerName Website-name
               DocumentRoot /var/www/html/site1
               ErrorLog ${APACHE_LOG_DIR}/error.log
               CustomLog ${APACHE_LOG_DIR}/access.log combined
           #    Redirect / https://your-https-site-name.com/   ## if your site is running as https
           
<Directory "/var/www/html/site1">
          Options Indexes FollowSymLinks
          AllowOverride all
          Allow from all
          Require all granted
    </Directory>
</VirtualHost>
```

Also, add additional headers in production.

save and exit.

enable the site

```plaintext
a2ensite 000-default.conf
```

for apache to work properly enable modules

```plaintext
sudo a2enmod rewrite 
sudo a2enmod ssl
sudo a2enmod php
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_ajp
sudo a2enmod rewrite
sudo a2enmod deflate
sudo a2enmod headers
sudo a2enmod proxy_balancer
sudo a2enmod proxy_connect
sudo a2enmod proxy_html
```

now restart apache

```plaintext
systemctl restart apache2
```

go to the browser and access the site

```plaintext
http://<IP-here>/phpinfo.php
```

You should see the PHP page.

## **Install Mysql Database**

To install `MySQL 8.0` ( ubuntu 20 and 22 come with Mysql 8.0 repo)

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

**Add** MySQL user and database and provide permissions.

## **WordPress Installation**

Since WordPress is a PHP site. we will assume WordPress site data.

go to the home folder of the user

```plaintext
su - ubuntu
cd public_html
```

Download WordPress from the official website

```plaintext
wget https://wordpress.org/latest.zip
```

unzip the folder

```plaintext
unzip latest.zip
```

If this extraction creates another folder change in the Apache config.

```plaintext
cd <wordpress directory>
```

edit the wp-config.php

```plaintext
vim wp-config.php
```

edit the database details. save and exit.

open the site in browser.

```plaintext
http://<IP-ADDRESS>
```
