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:
Requests will be pointed to the Apache web server whenever a user visits your website.
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.
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
sudo adduser ubuntu
switch user and create a folder public_html
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
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.
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.
sudo apt install php8.0-gd
Install Apache2
Install apache2 webserver
sudo apt install apache2 -y
Go to the Apache
site directory and add the site data symlink
cd /var/www/html
now create a symlink
ln -s /home/ubuntu/public_html /var/www/html/site1
switch user and add data file
su - ubuntu && cd public_html
For now, we are adding phpinfo file
vim phpinfo.php
add the following data
<?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
exit
Edit the configuration of Apache in sites-available
vim /etc/apache2/sites-available/000-default.conf
Paste the following
<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
a2ensite 000-default.conf
for apache to work properly enable modules
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
systemctl restart apache2
go to the browser and access the site
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)
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
su - ubuntu
cd public_html
Download WordPress from the official website
wget https://wordpress.org/latest.zip
unzip the folder
unzip latest.zip
If this extraction creates another folder change in the Apache config.
cd <wordpress directory>
edit the wp-config.php
vim wp-config.php
edit the database details. save and exit.
open the site in browser.
http://<IP-ADDRESS>