install wordpress php 7 debian

How to Install WordPress with PHP 7.1 and Nginx on Debian 9

Spread the love

In this guide, we will walk you through the basic installation process of WordPress on a Debian 9 VPS with Nginx, MariaDB and PHP 7.1. WordPress is the most popular CMS in the world with unlimited customization options. This should work on other Linux VPS systems as well but was tested and written for Debian 9.

1. Update the system and install necessary packages.

apt -y update && sudo apt -y upgrade
sudo apt install apt-transport-https lsb-release ca-certificates wget

2. Install MariaDB 10.1

sudo apt-get install mariadb-server

When the installation complete, run the following command to secure your installation:

mysql_secure_installation

Next, we need to create a database for our WordPress instance.

mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE wpdb;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'localhost' IDENTIFIED BY 'wpuser_passwd';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

3. Install and configure PHP 7.1

PHP 7.1 is not available via the default Debian repositories, so we will add the “packages.sury.org/php” repository, update the system and install the PHP 7.1 packages.

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update

To install the latest version of PHP 7.1 and all necessary modules, run:

sudo apt install php7.1-common  php7.1-readline php7.1-fpm php7.1-cli php7.1-gd php7.1-mysql php7.1-mcrypt php7.1-curl php7.1-mbstring php7.1-opcache php7.1-json 

Change few default PHP settings:

sudo sed -i "s/memory_limit = .*/memory_limit = 256M/" /etc/php/7.1/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.1/fpm/php.ini
sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.1/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.1/fpm/php.ini

PHP-FPM process manager has three choices: Static, Dynamic and Ondemand. The default setting for the process manager is “dynamic”, we will change it to “ondemand”.
Rename the default FPM  pool configuration file and create a new one:

sudo mv /etc/php/7.1/fpm/pool.d/www.conf /etc/php/7.1/fpm/pool.d/www.conf.org
sudo nano /etc/php/7.1/fpm/pool.d/www.conf
[www]
user = www-data
group = www-data
listen = /run/php/php7.1-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s
pm.max_requests = 200
chdir = /

Restart PHP-FPM:

sudo systemctl restart php7.1-fpm 

4. Install and configure Nginx

Install Nginx from the official Debian repositories:

sudo apt -y install nginx

Create a new Nginx server block with the following content:

nano /etc/nginx/sites-available/example.com
server {
  server_name example.com;
  listen 80;
  root /var/www/html/example.com;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  index index.php;

  location / {
    try_files $uri $uri/ /index.php?q=$uri&$args;
  }

  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }

  location ~ /\.ht {
    deny  all;
  }

  location ~ \.php$ {
    fastcgi_index index.php;
    fastcgi_keep_conn on;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

Activate the server block by creating a symlink:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Test the Nginx configuration and restart the server by running the following commands:

sudo nginx -t
sudo systemctl restart nginx

5. Install WordPress

Create a new directory for your WordPress site:

sudo mkdir -p /var/www/html/example.com

Download and extract the WordPress package:

wget -q -O - http://wordpress.org/latest.tar.gz | sudo tar -xzf - --strip 1 -C /var/www/html/example.com

Set the correct permissions:

sudo chown www-data: -R /var/www/html/example.com

Finally, run the WordPress installation script by accessing the URL in your web browser of choice. http://example.com/, enter the details for the database we created earlier in this post and create your WordPress admin user.


Of course, you don’t have to Install WordPress with PHP 7.1 and Nginx on Debian 9 if you use one of our WordPress Maintenance Services, in which case you can simply ask our expert Linux admins to Install WordPress with PHP 7.1 and Nginx on Debian 9 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on How to Install WordPress with PHP 7.1 and Nginx on Debian 9, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

3 thoughts on “How to Install WordPress with PHP 7.1 and Nginx on Debian 9

  1. Thanks for an excellent tutorial. Followed it and WordPress was installed flawlessly. There are other tutorials on web but I found it to be the best one if someone installs PHP7 on Debian 9.

    Just 1 simple issue: The server of packages.sury.org is extremely slow. When I installed PHP, data transfer was at I think just 30-40kbps. But it got installed in a few minutes.

    Thank you.

  2. Hello
    Thanks for describing all the steps.
    I would add additional deny rule to the virtualhost:

    # Deny public access to wp-config.php
    location ~* wp-config.php {
    deny all;
    }

  3. Hello please keep in mind that nginx has a default server configuration that is serving php-files by default!

    You should delete the default configuration:

    rm /etc/nginx/sites-enabled/default

    Else someone could download *.php files (eg. configuration files) by using your ip-address as url!

Leave a Reply to ducho Cancel reply

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