Installing PHP7 with nginx and MySQL on Debian 8 - Part 1

Table of Contents

  1. Pre-requists
  2. Install nginx
  3. Install PHP7 FastCGI with PHP-fpm
  4. Install MySQL

Pre-requists

First things first, I've setup my Debian 8 with a user and I will be running my commands through sudo. On a fresh Debian 8 you will need to install sudo using the command as root user:

apt-get install sudo

Once installed you will need to add the user to sudo group by using either the useradd or usermod command. As I setup my Debian with a user I need to use the usermod command:

usermod -a -G sudo systemadmin

-a appends the user -G adds to users secondary groups

Now that I've got sudo access for my user I will switch back to my user, then log out and back in again to begin the next step.

Install nginx

Installing nginx is relatively easy, using the command:

sudo apt-get install nginx

This will install nginx to the version available in the repo you have configured on Debian. However the version may be on the low side so we will be using a different repo for nginx.

For the purpose of this guide I will be adding a file in the /etc/apt/sources.list.d/ called webserver.list and the contents I need for this are:

deb http://nginx.org/packages/mainline/debian/ jessie nginx
deb-src http://nginx.org/packages/mainline/debian/ jessie nginx

First is a definition for binaries and second is for soruces and we will be getting from the mainline debian sources for Jessie.

Once you have the sources added you will need the public signing key for nginx, to get this you need to run the following command:

wget -q -O- http://nginx.org/keys/nginx_signing.key | sudo apt-key add -

wget will fetch the key assign it to - and pass it to the second command and which will add the key to apt.

All good! So now we can do the following:

sudo apt-get update
sudo apt-get remove nginx nginx-full nginx-common
sudo apt-get install nginx

This will now remove your nginx should you have installed it and install nginx as the newest version available for Jessie.

Now run:

sudo nginx -v

Will output the version of nginx, mine is 1.9.15 as of testing.

Install PHP7 Fast-cgi with PHP-fpm

Luckily for us PHP7 is part of the DotDeb repo which means we can use apt to get all php7-* modules. Next up we need to add the dotdeb repo to the list of soruces. So as before we will need to edit the /etc/apt/sources.list.d/webserver.list and add the following:

deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all

Once added we just need to run the command to add the key to apt using the command:

wget -q -O- https://www.dotdeb.org/dotdeb.gpg | sudo apt-key add -

For this install I am going to use PHP-fpm, command line interface, curl, mysql, mcrypt and image gd. You can then choose to add other modules by using the apt-get install. If you want to search for modules use the command:

sudo apt-cache search php7

This will then list all of the available modules available.

Once you are ready run the install:

sudo apt-get install php7.0-cli php7.0-fpm php7.0-curl php7.0-mysql php7.0-mcrypt php7.0-gd php7.0-common

Doing this will install all dependacies too so it might take some time.

Once complete you will be ready to configure.

Install MySQL

Installing MySQL is the easier of the steps to complete. Simply run the command:

sudo apt-get install mysql-server

The install will begin and you can fill out the required fields to setup MySQL.

For this you should be asked for a root password, add a password in and then continue.

Once completed you will want to secure MySQL by using:

sudo mysql_secure_installation
  • Say 'no' to changing my root password
  • Removed anonymos users
  • Disallowed root remote login (we will create a user later)
  • Remove the test database and access to it
  • Reload privileges

Now that you have completed that login to your MySQL server using:

mysql -u root -p

Type your password in and then you should see:

mysql>

This means you are logged into the MySQL server via command line, so run:

status

This will show you your server status.

Conclusion

So now we have nginx install with PHP7 and php-fpm and we have a MySQL server running. Next we will need to configure these and I will go through the basics in the next part of this article.

Posted in Featured on May 09, 2016