Setup the Server

How to setup Apache + PHP + PHP-FPM on Ubuntu 20

Add current user to www-data group

sudo usermod -a -G www-data `whoami`

This action requires to sign out and sign in again to take effect.

To prevent running composer or npm in sudo mode. This also helps with permission denined from application when they try to write file due to the folder does not belong to www-data group. As the default group when create or pull request is root.

Install Apache + PHP

By default, ubuntu 20 with x86 architect, when you install php, it will install php8 while for Arm architect, this will be 7.4.

For install specific version, add version number after php.

Ex. sudo apt install php7.4 or sudo apt install php7.4-cli

For installing php7.4 version on x86 Ubuntu 20

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

For installing latest Apache2 version

sudo add-apt-repository ppa:ondrej/apache2

Install Apache2 + PHP

sudo apt update
sudo apt install apache2 php -y
sudo apt install php-cli php-pear php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php php-bcmath php-simplexml -y

Setup PHP-FPM

Install required libraries

Enable Apache Mods

Add Code Block in <VirtualHost>

Restart Apache Service

To check is FPM is working

Create a file with

At the Server API, if it says FPM/FastCGI. Then, it is working.

Install Composer

Install composer command

Install NPM

Use code below to get a specific latest version instead. (The code below is for Nodejs 16)

If any is fine, you can just use install nodejs like normal.

Git Clone Project

Example happenn-api.git project.

Enable rewrite mod for Apache2 so .htacess file is working.

Add rewrite rules and change DocumentRoot in /etc/apache2/sites-available/000-default.conf

As Laravel and Lumen using public folder instead of its root for handling request.

Rewrite rules for making .htacess to start working.

Prepare directory

Re-create html folder with a user group permission of www-data

This will make it possible to do composer install and npm install without sudo and let Laravel or Lumen to generate cache and log without error.

Clone git

Use auth token from Github to make it possible to just use git pull later on without requiring password.

Allow permissions for storage system

Cron on Reboot the Server

Setup Bash File

Setting up a bash file for a cron to execute the bash file.

This can be located anywhere but should be at $HOME

Depend on the project as the example above is fore Happenn API. The code should aim to deploy the latest code on reboot the server. As, this is mostly for auto-scaling of EC2.

This will give permission to be executable.

Tell cron to execute the file on reboot

Do not using sudo, as it may not works.

Add below code at the bottom of the file

Last updated