Published on
Week 3 -

Setting Up a Fresh Ubuntu Server for PHP Development

Authors

This guide will help you set up a fresh Ubuntu server with Apache, PHP, MariaDB, and phpMyAdmin, making it fully ready for PHP development and deployment.


Step 1 — Installing Apache and Updating the Firewall

Update the package list and install Apache:

sudo apt update  
sudo apt install apache2  

Configure the firewall to allow web traffic:

sudo ufw app list  
sudo ufw app info "WWW Full"  
sudo ufw allow in "WWW Full"  

Now, accessing localhost should display the Apache default page.

CURL is useful for various future tasks:

sudo apt install curl

Step 2 — Installing MariaDB

Install MariaDB server:

sudo apt install mariadb-server  

Secure the installation:

sudo mysql_secure_installation  

Create a new MariaDB user:

sudo mariadb  
GRANT ALL ON *.* TO 'username'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD' WITH GRANT OPTION;  
FLUSH PRIVILEGES;  
exit  

Step 3 — Installing PHP

Install PHP and required modules:

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

Edit Apache settings to prioritize PHP files:

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

Modify the DirectoryIndex line:

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

Restart Apache:

sudo systemctl restart apache2  

Step 4 — Installing phpMyAdmin

Install phpMyAdmin and necessary PHP modules:

sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl  

During installation:

  • Select apache2 when prompted. (Press SPACE to select, then TAB, then ENTER.)
  • Choose Yes when asked to use dbconfig-common.
  • Set a MySQL application password for phpMyAdmin.

Enable the mbstring PHP extension:

sudo phpenmod mbstring  

Restart Apache:

sudo systemctl restart apache2  

Step 5 — Running a Laravel Project

Install Composer

Laravel requires Composer, a dependency manager for PHP. Install it globally:

sudo apt install composer  

Set Up a Laravel Project

Navigate to the web root directory:

cd /var/www/html  

Create a new Laravel project:

composer create-project --prefer-dist laravel/laravel project-name  

Set the correct permissions:

sudo chown -R www-data:www-data /var/www/html/project-name  
sudo chmod -R 775 /var/www/html/project-name/storage /var/www/html/project-name/bootstrap/cache  

Configure Apache for Laravel

Create a new virtual host configuration:

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

Add the following content:

<VirtualHost *:80>  
    ServerAdmin webmaster@localhost  
    DocumentRoot /var/www/html/project-name/public  
    <Directory /var/www/html/project-name/public>  
        AllowOverride All  
        Require all granted  
    </Directory>  
    ErrorLog ${APACHE_LOG_DIR}/error.log  
    CustomLog ${APACHE_LOG_DIR}/access.log combined  
</VirtualHost>  

Enable the site and rewrite module:

sudo a2ensite laravel  
sudo a2enmod rewrite  
sudo systemctl restart apache2  

Your Laravel project should now be accessible via the server's IP address or domain.


Conclusion

Your Ubuntu server is now fully set up for PHP development. With Apache serving web pages, PHP executing scripts, MariaDB handling databases, and phpMyAdmin providing a GUI for database management, you’re ready to deploy your PHP projects!