Happenn
  • Introduction
  • System Architect
  • For Dev
    • AWS
      • Domain & CNAME
      • Rescale Instance
      • Request AWS Certificate
      • Generate Certificate with Certbot
      • S3
      • SSH & FTP
      • Auto Scaling
      • RDS MySQL
    • Digital Ocean
      • Virtual Host & Certificate
      • SSH & FTP
    • Server
      • Git
      • Setup the Server
    • Code
      • Laravel/Lumen
      • Vue.js
      • Webpack
      • JavaScript Library
        • Lodash
        • axios
        • Moment.js
      • Code Style Guide
      • Useful Tools
    • HAM Admin
      • Install locally
      • Deploy
    • Happenn API
      • Install locally
      • Install DB locally
      • Make DB changes
    • Happenn IO
      • Install locally
    • Happenn Virtual
      • Install locally
      • Folders and file structure
      • Deploy code
        • Happenn AWS
        • Delegia AWS
    • Happenn Event app
      • Deploy
      • Install locally
Powered by GitBook
On this page
  • Add current user to www-data group
  • Install Apache + PHP
  • Setup PHP-FPM
  • Install Composer
  • Install NPM
  • Git Clone Project
  • Cron on Reboot the Server
  1. For Dev
  2. Server

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

sudo apt install php-fpm libapache2-mod-fcgid -y

Enable Apache Mods

sudo a2enmod rewrite actions fcgid alias proxy_fcgi

Add Code Block in <VirtualHost>

<FilesMatch \.php$>
    SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>

Restart Apache Service

sudo service apache2 restart

To check is FPM is working

Create a file with

<?php phpinfo(); ?>

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

Install Composer

Install composer command

sudo apt install wget unzip -y
cd $HOME
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo chmod 755 /usr/local/bin/composer

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.

for latest or xxx version
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs

Git Clone Project

Example happenn-api.git project.

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

sudo a2enmod rewrite headers
sudo service apache2 restart

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

DocumentRoot /var/www/html/public

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

<Directory /var/www/html/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Rewrite rules for making .htacess to start working.

sudo service apache2 restart

Prepare directory

sudo rm -rf /var/www/html
sudo mkdir /var/www/html
sudo chmod -R 774 /var/www/html
sudo chown -R www-data:www-data /var/www/html
cd /var/www/html

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

git clone https://<auth-token>@github.com/happenn-virtual/happenn-api.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

sudo chmod -R 777 ./storage

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

on-start.sh
cd /var/www/html
git pull origin master
composer install

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.

sudo chmod -x on-start.sh

This will give permission to be executable.

Tell cron to execute the file on reboot

Do not using sudo, as it may not works.

crontab -e

Add below code at the bottom of the file

@reboot /home/ubuntu/on-start.sh
PreviousGitNextCode

Last updated 3 years ago