Skip to content

Instantly share code, notes, and snippets.

@247software-sanket-gandhi
Last active August 3, 2024 07:17
Show Gist options
  • Save 247software-sanket-gandhi/3268c91e04c136e391ee5fef42eef459 to your computer and use it in GitHub Desktop.
Save 247software-sanket-gandhi/3268c91e04c136e391ee5fef42eef459 to your computer and use it in GitHub Desktop.
Ubuntu 18.04 - Install PHP 7.2, PHP-FPM 7.2, Nginx

Install NGINX

# Install software-properties-common package to give us add-apt-repository package
sudo apt-get install -y software-properties-common

# Install latest nginx version from community maintained ppa
sudo add-apt-repository ppa:nginx/stable

# Update packages after adding ppa
sudo apt-get update

# Install nginx
sudo apt-get install -y nginx

# Check status
sudo service nginx status

# Start nginx if it is not already running
sudo service nginx start

# At this point you should have NGINX up and running, open http://localhost on your 
# browser and you should see a welcome page from NGINX

Install PHP 7.2 and PHP 7.2-FPM

# check what is the latest PHP version supplied by UBUNTU
sudo apt-cache show php

# You should get something like below where 'Version' would tell us the latest version available in my case is 7.2

# Package: php
# Architecture: all
# Version: 1:7.2+60ubuntu1
# Priority: optional
# Section: php
# Source: php-defaults (60ubuntu1)
# Origin: Ubuntu
# Maintainer: Ubuntu Developers <[email protected]>
# Original-Maintainer: Debian PHP Maintainers <[email protected]>
# Bugs: https://bugs.launchpad.net/ubuntu/+filebug
# Installed-Size: 12
# Depends: php7.2
# Filename: pool/main/p/php-defaults/php_7.2+60ubuntu1_all.deb
# Size: 3084
# MD5sum: 819cca55fe70ba29e41348bd2719cfb4
# SHA1: d42454bf1d4a552725693f9ec72cbd60eae9011a
# SHA256: fb00497dae1721eb9b0f1092f7758a9fd7b37815f3e36e44168ff4109121b834
# Description-en: server-side, HTML-embedded scripting language (default)
# PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
# open source general-purpose scripting language that is especially suited
# for web development and can be embedded into HTML.
# .
# This package is a dependency package, which depends on Ubuntu's default
# PHP version (currently 7.2).
# Description-md5: 778bb04d56a141a57b6ce7e9e2bd3d76
# Supported: 5y

# If you do not get php 7.2 then add below ppa which gives us the latest php version 7.2 then check again
sudo add-apt-repository ppa:ondrej/php

# Lets now install php7.2 and some important modules which we will need.
sudo apt-get install php7.2-cli php7.2-fpm php7.2-curl php7.2-gd php7.2-mysql php7.2-mbstring zip unzip

# Once done all basic modules will be installed now, lets check the version now
php -v

# Lets also check if the PHP7.2-FPM is running, if not started it
sudo service php7.2-fpm status

# If php-fpm is not running 
sudo service php7.2-fpm start

# Now we need to configure the Gateway so that PHP-FPM uses the UNIX socket for executing PHP files.
# For this we need to check where is the PHP7.2-fpm service running from and where are their configurations stored

sudo ps aux | grep php # (this will list php process)

# You will see the output something like below depending on how many processes are running

# root     25121  0.0  0.1 427436 25088 ?        Ss   11:42   0:00 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
# www-data 25133  0.0  0.0 429736 12276 ?        S    11:42   0:00 php-fpm: pool www
# www-data 25134  0.0  0.0 429736 12276 ?        S    11:42   0:00 php-fpm: pool www

# You will see the php-fpm master process is its location as /etc/php/7.2/fpm/php-fpm.conf. We now need to check the socket details
# from the PHP7.2-fpm by view this file.

vim /etc/php/7.2/fpm/php-fpm.conf

# You will see at the end of the file something like 'include=/etc/php/7.2/fpm/pool.d/*.conf' which means this process manager is using configurations from
# the pool.d directory. When viewing the /etc/php/7.2/fpm/pool.d/www.conf you will see that the PHP7.2-fpm (process manager) is 
# listening to the socket at 'listen = /run/php/php7.2-fpm.sock'
# Copy the socket location i.e. /run/php/php7.2-fpm.sock and we will use this socket to tell nginx to use this socket

sudo vim /etc/nginx/site-available/default

# You will need to tell NGINX to process the index.php files as well so replace the following line

# index index.html index.htm index.nginx-debian.html;
# to
# index index.html index.htm index.php;

# Also you will see something like the following block for 'location'

#location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
#       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
#}

# Make changes so to the above block so you have the following one:

location ~ \.php$ {
       include snippets/fastcgi-php.conf;
#
#       # With php-fpm (or other unix sockets):
       fastcgi_pass unix:/run/php/php7.2-fpm.sock;
#       # With php-cgi (or other tcp sockets):
#       fastcgi_pass 127.0.0.1:9000;
}

# Note we have uncommented the location, and fastcgi_pass lines and also pasted the correct socket that
# is used by our php7.2-fpm (process manager)

# lets now login as root and restart both nginx and php7.2-fpm
sudo su
sudo service nginx reload
sudo service php7.2-fpm restart

# Now lets create a index.php file within the document root i.e. root /var/www/html;
touch /var/www/html/index.php


# and put phpinfo() and save it.
# now open http://localhost and you should see the screen with all php informaiton

# You may run into permission issue while creatiing index.php file in /var/www/html/ so inorder to fix then add your current user in www-data group
sudo gpasswd -a "$USER" www-data

# Now whenever you will write a new file then it will have proper permission. In order to change previously created files run -
sudo chown -R "$USER":www-data /var/www
find /var/www -type f -exec chmod 0660 {} \;
sudo find /var/www -type d -exec chmod 2770 {} \;

All your project goes inside the /var/www/html directory. BUT it is good practice (and reduce lot of headache of permission) by creating symlink of your project in the html directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment