Ubuntu Server Setup with Nginx, PHP-FPM and MariaDB

Ubuntu server Over the course of the last decade or so, I’ve written and rewritten brief tutorials on setting up various server software products on an Ubuntu server. Today, this is all of them put together and shortened, making it as brief as possible.

With a desktop version of Ubuntu, some of it can be done through the package manager or software store. When it’s an Ubuntu server, which shouldn’t have a graphical user interface, it all has to be done from a command line in a terminal session.

Even when I’m using a desktop version of Ubuntu or a Debian/Ubuntu derivative for web development, I still install everything I need from the command line. It’s faster and easier to cut and paste the commands I need to execute than to wait on some other kind of interface.

Preparing for Various Software Installations on your Ubuntu Server

On many Ubuntu server setups, the only daemon (or software server) available from the beginning is SSH. It’s through an SSH terminal session that you install everything else.

If you already have a superuser set up, you can skip using “sudo” in front of every command by using the command “sudo su” and entering your user password. It’s the first user set up, of course. I’ll use sudo in the commands below, if you prefer to do it that way.

Most of the software sources are in the Ubuntu or Debian repositories, but some (or newer) sources are provided by one PPA (personal package archive) or another. Before you can add a PPA from the command line (unless you like editing files manually), you need to do this:

sudo apt install python-software-properties

The repository versions of Nginx, PHP and MariaDB may not be as up-to-date as you like. If they are, you can skip adding the PPAs. The last time I did it on my desktop PC, they were pretty up-to-date.

Add the PPA for Nginx:

sudo add-apt-repository ppa:nginx/stable

Before you add the PPA for PHP, do this if your Ubuntu server isn’t set up to use English. Otherwise, you can skip it (do it anyway, just to be safe):

sudo apt install -y language-pack-en-base

Add the PPA for PHP:

sudo add-apt-repository ppa:ondrej/php

You don’t need to add a PPA for MariaDB (a drop-in replacement for MySQL). You should install “git” and “bc” so you can pull packages from github and gitlab later on:

sudo apt install git bc

Now, do this to make sure your repository lists are up-to-date:

sudo apt update

Install and Configure Nginx on your Ubuntu Server

The plain Nginx web server, without a bunch of extra packages, is installed with:

sudo apt install nginx

If you need those extras, install the other package:

sudo apt install nginx-extras

Edit the main configuration file:

sudo nano /etc/nginx/nginx.conf

Just before the closing curly brace in the “event” section, insert:

use epoll;

Within the “http” section, under the gzip settings, un-comment all of it. Then remove this from the end of the http section:

include /etc/nginx/conf.d/*.conf;

You can add configuration files manually from within each server section (also called the virtual host settings).

Under the server section, add this line:

add_header Vary: Accept-Encoding;

There isn’t a built-in facility on the Ubuntu server for enabling and disabling virtual hosts with Nginx. You have to edit the configuration files under “/etc/nginx/sites-available/” and then symbolically link them to “/etc/nginx/sites-enabled/”. You should install nginx_modsite and use its commands to make things easier. Put all of your server sections in the “default” server configuration file if you only have one website. It doesn’t make sense to do it otherwise unless you have multiple websites set up.

Don’t do it now, but you can start Nginx with:

sudo service nginx start

Install and Configure PHP on your Ubuntu Server

Unless you’re planning to do something unusual, you should only install PHP-FPM (the PHP FastCGI Process Manager). Some other packages, along with PHP-CLI, will be installed along with it:

sudo apt install php7.0-fpm

If you’re planning to use a CMS like WordPress, install these other packages. They’re not otherwise installed:

sudo apt install php7.0-dev php7.0-mysql php7.0-xmlrpc php7.0-curl php7.0-gd php7.0-mcrypt php7.0-tidy

Edit the PHP configuration file:

sudo nano /etc/php/7.0/fpm/php.ini

Find and change “upload_max_filesize” and “post_max_size” to read:

upload_max_filesize = 20M
post_max_size = 20M

Find and change “cgi.fix pathinfo” to read:


Edit the PHP-FPM configuration file:

sudo nano /etc/php/7.0/fpm/pool.d/www.conf

When you’re all set and done and load your website and it doesn’t work, you may have change the “listen.mode” to read:

listen.mode = 0666

You can use either option for “listen” but you have to make sure it matches what you set in your Nginx server section for PHP.

If you want to know the best way to run PHP-FPM on small to medium websites, please read “A better way to run PHP-FPM“.

Don’t do it now, but you can start PHP with:

sudo service php7.0-fpm start

Install and Configure MariaDB on your Ubuntu Server

These are the only things you need to install:

sudo apt install mariadb-server mariadb-client

During the installation, give the “root” user a password. You’ll need it when you add databases. I recommend Adminer instead of phpMyAdmin. It’s a single file and you can easily remove it when you don’t need it. Go to the Adminer website to get the latest version information. Then go to the document root of your website with the command line and then use “wget” with the URL for it.

Open it in the web browser and do your thing. If it won’t let you login as root, I suggest you start MariaDB with “mysql” and then create a new user, granting it all the privileges, and use it instead. That’s what I had to do on more than one occasion.

Don’t do it now, but you can start MariaDB with:

sudo service mysql start

After you start it, run this:

sudo mysql_secure_installation

Why “mysql” instead of “mariadb”? I don’t know but it’s probably because of it being a drop-in replacement. If you install MySQL first and then install MariaDB, the MySQL binaries will be uninstalled. The directories and configuration files will be left, with some being used by MariaDB.

Before Starting any of the Daemons on your Ubuntu Server

This is only the beginning. You can start all three daemons and they’ll run but you really can’t do anything yet. You need to set up a website (or multiple websites) on your Ubuntu server.

If you want to tweak things further (and you should), go to these places and read:

Originally published in August of 2016.

October 23, 2017

Web Development

Previous and Next Articles:

« »