Install PHP 7.1 with Nginx on Ubuntu 16.04 VPS
PHP 7.1 comes with a lot of new features and improvements, as a result of which many developers use it for their projects. In this article, we are going to show you how to install PHP 7.1 with Nginx on an Ubuntu 16.04 VPS.
Step 1: enable PPA
First of all, connect to the Linux VPS via SSH and enable Ondrej’s PPA:
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt-get update
Step 2: Install PHP 7.1
After enabling the PPA, you can proceed and install PHP 7.1 using the following command:
sudo apt-get install php7.1
Step 3: Find and Install Specific PHP 7.1 Modules
You will also need to install the required dependencies too. However, if you want to install a custom PHP7.1 module, you can find it if available with the following command:
sudo apt-cache search php7.1
Step 4: Install the most commonly used modules
To install PHP7.1 including some of the most commonly used modules you can use the following command:
sudo apt-get install php7.1 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
Step 5: configuring php.ini file
After the installation is complete, you need to edit the file php.ini
… Find the config file:
php --ini |grep Loaded Loaded Configuration File: /etc/php/7.1/cli/php.ini
Edit the file with your favorite text editor:
sudo nano /etc/php/7.1/cli/php.ini
Make the following changes:
cgi.fix_pathinfo=0
Then restart the PHP-FPM service:
sudo systemctl restart php7.1-fpm.service
Step 6: Installing Nginx on Ubuntu 16.04
Installing Nginx on Ubuntu VPS is straightforward. Run the following command to install it:
sudo apt-get install nginx
Create a nginx virtual server block for a domain:
sudo nano /etc/nginx/sites-available/example.ru
Paste the following content:
server { listen 80; server_name example.ru www.example.ru; root /var/www/example.ru; index index.php; location / { try_files $uri $uri/ =404; } location ~ .php$ { fastcgi_pass unix:/run/php/php7.1-fpm.sock; include snippets/fastcgi-php.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } location ~ /.ht { deny all; } }
You must of course replace example.ru to your domain name. Save and close the file. To enable server block in Nginx you need to create a symbolic link site-enabled
… Use the following command to create a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.ru /etc/nginx/sites-enabled/example.ru
Check if there are any errors in the configuration:
sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If the syntax is ok and there are no errors, you can restart Nginx.
sudo systemctl restart nginx.service
Enabling Nginx and PHP-FPM at system boot:
sudo systemctl enable nginx.service sudo systemctl enable php7.1-fpm.service
Further steps
Once you’ve installed PHP 7.1 and Nginx on your Linux VPS, you can follow our guide on how to secure your LEMP stack.