Instructions to deploy PHP Application on Ubuntu VPS

Tram Ho


  • When completing a project, the next job is deploying it so that everyone can view your app. For projects that are academic, you can deply it on free servers (for example, Heroku ), but because it’s free, it still has many problems. You deploy a Laravel project using Ubuntu VPS.

What is VPS?

  • VPS (Virtual Private Server) is a form of virtual server created by the method of dividing a physical server into many different servers with similar features as a private server. And run as resource sharing from the original physical server. And each VPS will have a separate CPU part, RAM capacity, HDD capacity, IP address and operating system, users have full root control and can restart the system whenever they want. .

What are the specifications to know when renting a VPS virtual server?

  • Server RAM: To help the VPS server work smoothly and stably, choose a product with as much RAM as possible. Most RAM types have similarities in performance. When buying or renting a VPS, you should carefully ask the supplier if the RAM is for the physical server or virtual server.
  • What is the VPS virtual server SWAP? : SWAP is a virtual memory that stores old processing actions if the RAM memory is full. SWAP itself is a storage space on the hard drive rather than an independent memory. Not all VPS support SWAP memory but only XEN VPS support SWAP.
  • Hard disk (disk): Is not stored will be used to save the installation files of the operating system and the files of the website source code you save on it. Current drives are divided into 2 types: HDD and SSD. SSDs are usually more expensive than HDDs.
  • Band Width: Think of the bandwidth as well as the width of the street. The wider the street, the more convenient the traffic and vice versa. Bandwidth providers usually provide a minimum bandwidth of 10 – 20 Mbps.
  • What is the Up-time time of VPS virtual servers ?: Another note you need to know when choosing to buy or rent the server is its Up-time time. The up-time time of VPS is usually estimated from its uptime. VPS uptime from 99.95 to 99.9%, you can accept buying.
  • What is the virtual server operating system VPS? VPS virtual server has 2 popular operating systems: Linux and Window. Linux is user friendly, supporting more applications with less cost than Window. However, working with Dot Net or Visual Studio, you should choose VPS Window.

Where does VPS get it?

  • Because of that genuine, most VPS must buy. There are some famous suppliers in the world that I know are:

Guide to deploy

Log in

  • Open Terminal on Ubuntu and type the command:

(with username and ip are the parameters after VPS registration provided)

Then enter the password (VPS registration provided) After logging in is complete with this interface

Install the components needed to run PHP Application

Install nginx

Install PHP + PHP-FPM

install MySQL

After you have installed the components needed to run PHP Application, we need to configure nginx again with php-fpm , you type the command

It appears like this

Then navigate to the location ~ .php$ and edit the paragraph as follows:

Create user deploy – Serve auto deploy (Optional)

  • To perform deploying tasks on a specific user. Isolate with other user accounts.
  • Need to generate SSH key to be able to pull source code via SSH (for auto deploying)

Convert the user running nginx process to user deploy

  • To avoid file / folder permission issues after deploying. Because Nginx processes need read, write and execute permissions on the “storage” and “bootstrap / cache” directories.

Convert the user running php-fpm process to user deploy

  • To avoid file / folder permission issues after deploying. Because php-fpm processes need read, write and execute permissions on the “storage” and “bootstrap / cache” directories.

Add deploy key to the repository

  • Go to the repository on github add deploy key


ssh to the server

Pull the code on your github repo

Install more packages for setup

  • install composer

  • install node/npm

  • install yarrn

Setup packages and config file .env

  • Move into the project

Run migrate

The last and most important step is the Setup virtual host to point to the project directory

  • Create config deploy file in nginx and configure it

  • Because by default nginx it is reading the file /etc/nginx/sites-enabled/default it is necessary to delete the file /etc/nginx/sites-enabled/default and symlinks /etc/nginx/conf.d/deploy.conf to /etc/nginx/sites-enabled to defaul read config in deploy.conf

  • Restart nginx, php-fpm:

Go to your browser and type the ip address and see the result 😅

Share the news now

Source : Viblo