Setting up an Ubuntu Server with Nginx, PHP-FPM and MariaDB
Over the course of the last 10 years, I’ve written and rewritten brief tutorials on setting up various server software products on an Ubuntu server. Those tutorials were more for my benefit than anyone else’s. Today, I’m going to combine them all into one tutorial, making it as brief as possible. It’s still mainly a resource benefit for me.
With a desktop version of Linux, this can all be done through a visual package manager. When it’s a server without a graphical user interface, it all has to be done from the command line in a terminal session. Even though I use Linux Mint as my preferred flavor of Linux at home, I still install stuff like this from the command line. It’s only a matter of copy, paste and enter.
Preparing for Various Software Installations on Ubuntu
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, skip using “sudo” in front of every command by doing this:
Enter the root password and you’re ready to rock without it. Just be careful.
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:
apt install python-software-properties
Add the PPA for Nginx:
Before you add the PPA for PHP, do this if your server isn’t set up to use English. Otherwise, you can skip it (do it anyway, just to be safe):
apt-get install -y language-pack-en-base LC_ALL=en_US.UTF-8
Add the PPA for 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 gitlabs later on:
apt install git bc
Now, do this to make sure your repository lists are up-to-date:
Install and Configure Nginx on Ubuntu
If you don’t need some of the extra packages, like GeoIP and purge, install the plain Nginx server:
apt install nginx
If you need those extras, install the other package:
apt install nginx-extras
Edit the main configuration file:
Just before the closing curly brace in the “event” section, insert:
Within the “http” section, under the gzip settings, un-comment all of that and insert this immediately after that group:
add_header Vary: Accept-Encoding;
Under the virtual host settings, remove this line:
You can include the files placed there anywhere and not necessarily at the http level.
There isn’t a built-in facility in Ubuntu 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 these commands to make things easier:
nginx_modsite -e nginx_modsite -d nginx_modsite -l
If you want to use SSL (and you should if logins are needed), you should read my article about getting a free SSL certificate. I mention all the settings needed for it.
Don’t do it now, but you can start Nginx with:
service nginx start
Install and Configure PHP on Ubuntu
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:
apt install php7.0-fpm
If you’re planning to use a CMS like WordPress, install these other packages. They’re not otherwise installed:
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:
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:
Find and change “listen.mode” to read:
listen.mod = 0666
You can use either option for “listen” but you have to make sure it matches what you set in your Nginx virtual hosts (called “server” in Nginx) 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:
service php7.0-fpm start
Install and Configure MariaDB on Ubuntu
This is the only thing you need to install:
apt install mariadb-server
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. Just go to the document root of your website and:
Open it in the web browser and do your thing.
Don’t do it now, but you can start MariaDB with:
service mysql start
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 behind, with some being used by MariaDB.
Before Starting any of the Daemons
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 your website (or multiple websites).
If you want to tweak things further (and you should), go to these places and read:
- The Nginx documentation area
- The PHP area for FastCGI Process Manager (FPM)
- The MariaDB learn section