ScAlAn

Setup nginx+php7.2fpm Ubuntu 18.04

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

# 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 PHP7.2 and PHP7.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.0

# Package: php
# Architecture: all
# Version: 1:7.0+35ubuntu6.1
# Priority: optional
# Section: php
# Source: php-defaults (35ubuntu6.1)
# Origin: Ubuntu
# Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
# Original-Maintainer: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
# Bugs: https://bugs.launchpad.net/ubuntu/+filebug
# Installed-Size: 11
# Depends: php7.0
# Filename: pool/main/p/php-defaults/php_7.0+35ubuntu6.1_all.deb
# Size: 2862
# MD5sum: c5db88de62d1dcb9679e9ae328d7c94a
# SHA1: 49d3f1d1850ac867fedf6fbc5635c33227dfde53
# SHA256: 0b1ccf22b958163669e677d6f660888eec7951d6eda3ae0f49a8e3409773cd87
# Description: server-side, HTML-embedded scripting language (default)
# Description-md5: b955c03ceec2872c327e77278c943d6a
# Phased-Update-Percentage: 90
# Supported: 5y


# Add Repository which gives us the latest php version 7.2
sudo add-apt-repository ppa:ondrej/php

# Lets now check what is the latest PHP version available now after the repository is added
sudo apt-cache show php

# You should now get something like below and notice the latest version is changed i.e Version: 1:7.2+60+ubuntu16.04.1+deb.sury.org+1

# Package: php
# Source: php-defaults (60+ubuntu16.04.1+deb.sury.org+1)
# Priority: optional
# Section: php
# Installed-Size: 12
# Maintainer: Debian PHP Maintainers <pkg-php-maint@lists.alioth.debian.org>
# Architecture: all
# Version: 1:7.2+60+ubuntu16.04.1+deb.sury.org+1
# Depends: php7.2
# Filename: pool/main/p/php-defaults/php_7.2+60+ubuntu16.04.1+deb.sury.org+1_all.deb
# Size: 5654
# MD5sum: 1089c40cb9763d667b1ba2bfb0b6b2c4
# SHA1: d34c619a14854708f43ae432daf5fe5e73737072
# SHA256: 6b2b24f7232f980818a688f66fd3f9759ac9d1eb0466dee30faa5b2f4c2b36df
# Description: server-side, HTML-embedded scripting language (default)
# Description-md5: 2ccdfdb6b598dc9bdf5572917b808dcb

# 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 php7.2-xml

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

php -v

# PHP 7.2.2-3+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Feb  6 2018 16:11:23) ( NTS )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
#    with Zend OPcache v7.2.2-3+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies


# Lets also check if the PHP7.2-FPM is running, if not start it

sudo service php7.2-fpm status

sudo service php7.2-fpm start  # (if the service isn't running already)

# Let's get socket in:
vi /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

vi /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 restart both nginx and php7.2-fpm

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

Done.

Leave a Reply

Your email address will not be published. Required fields are marked *