How to Setup Nginx with PHP 7.1 on Ubuntu 16.04 LTS / 18.04 LTS Server
I started playing around with Ubuntu 18.04 server recently.. I have it installed on VMware Workstation 14 Pro running on Windows 10… Here’s how I PHP 7.1 is used on both Ubuntu 16.04 and 18.04
If you’re going to be developing any PHP based applications, you’re mostly going to need PHP server scripts installed. PHP is an open source server scripting language use for creating dynamic, powerful web applications and websites.
PHP 7.1 is the latest stable version… If you’re running Ubuntu 16.04 then you probably running PHP 7.0… If you’re testing Ubuntu 18.04 then PHP 7.1 is available to you from Ubuntu repositories.
If you’re running Ubuntu 16.04 or 18.04, the steps below shows you how to configure Nginx to use PHP 7.1.
To install PHP 7.1 on Ubuntu, follow the steps below:
Step 1: Preparing Ubuntu 16.04 LTS
By default, PHP 7.1 isn’t available on Ubuntu 16.04… If you need PHP 7.1 on Ubuntu 16.04, you will have to install a third-party repository to upgrade to it… To do that, run the commands below. You only need to install the repository below on Ubuntu 16.04 LTS… 18.04 already has PHP 7.1 installed.
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php
Step 2: Update and Install PHP 7.1
Now that Ubuntu 16.04 is configured for PHP 7.1, run the commands below to install PHP 7.1 on both 16.04 and 18.04
sudo apt-get update sudo apt-get install php7.1 libapache2-mod-php7.1
Step 3: Install other PHP Modules
PHP 7.1 may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.
Run the commands below to add the below third party repository to upgrade to PHP 7.1
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php
Then update and upgrade to PHP 7.1
sudo apt update
Finally, run the commands below to install PHP 7.1 and related modules..
sudo apt-get install php7.1-fpm php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-fpm php7.1-ldap php7.1-tidy php7.1-recode php7.1-curl
Step 4: Setup Nginx HTTPS Server to Use PHP-FPM
Now that PHP 7.1 is installed, its default configuration file is located at /etc/php/7.1/fpm/php.ini
Run the commands below to open the config file and a great place to start it to change the values for the lines below
sudo nano /etc/php/7.1/fpm/php.ini
The value below are great settings to apply in your environments.
file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 100M max_execution_time = 360 cgi.fix_pathinfo=0 date.timezone = America/Chicago
Open Nginx site configuration file and configure Nginx to use PHP7.1-FPM
sudo nano /etc/nginx/sites-available/example.com
Then configure the it to look similar to this:
server { listen 80; listen [::]:80; root /var/www/html/example.com; index index.php index.html index.htm; server_name example.com www.example.com; client_max_body_size 100M; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ /\.ht { deny all; } }
Save your changes.
Step 5: Enable the new site
Run the commands below to enable the site you’re configuring.
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Finally, restart Nginx and PHP7.1-FPM services by running the commands below
sudo systemctl restart nginx.service sudo systemctl restart php7.1-fpm.service
That’s it!
You may also like the post below: