My custom setup up a server for php, mariadb, nginx

Every now and then I set up a new server and migrate a few sites onto it. I’ve often considered switching to managed hosting but never been willing to switch. I like having full root level access to the system too much.

So that aside I’ve decided to document my process for setting up a new server to serve some fairly standard php sites (e.g. wordpress/joomla/opencart) and my own custom php code.

My preferences for setup of this kind have changed a lot over the years since I first started messing about with web stuff in 2004. These days I prefer php-fpm and nginx to serve the pages and if a database is needed I was using mysql but this being the first server I’ve set up from scratch since mysql became an Oracle technology I’ve decided to switch to mariadb as it’s pretty much a drop in replacement and is not controlled by Oracle.

In terms of OS I tend to use either Debian or Ubuntu. I prefer Debian because it’s changes a bit less frequently. In the examples below I use Debian 8.

On to the setup.

I’ll assume you either know how to install the OS or like me have used a virtual server image to get started.

I start by logging in as root. It’s often considered bad practice to log on as root however I disagree as whenever I log in to the server it’s to perform some maintenance so I will need root level permissions. I would agree that it is bad practice of you are doing anything but managing the system though.

Having logged in, I like to have the command prompt reflect the name of the server I’m logged into. Therefore I edit the /etc/hostname file to contain my hostname. This is sometimes done during OS install but is often not done when installing from an image. I also edit the /etc/hosts file to reflect my hostname and fully qualified domain name. A hosts file typically looks like this (where <hostname> is your machines hostname): localhost <hostname>

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Now those who have some understanding of networking may wonder why the second line is set for the hostname and why it isn’t simply combined with the first line. Having wondered this myself I read some debian docs which explain that it is basically a workaround for some bugs in some software such as gnome. It only applies if you don’t have a static IP. In my case I do have a static IP so my hosts file looks a bit like this: localhost
<my.static.ip> <hostname> <>

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

I of course put my own values in for <my.static.ip>, <hostname> and <>.

Then I ensure the packages installed on the system are up to date by running:

apt-get update
apt-get dist-upgrade -y

Having set up my hosts file and updated to the latest packages I typically reboot, although it isn’t really necessary.

I then move on to configure email sending from the system using nullmailer and an smtp service like Send Grid who offer a free account.

I create a new API key for the server by signing in to my SendGrid account and using their interface to create the key. It’s important to save the generated API key somewhere as you can’t retrieve it again from SendGrid. That done I’m ready to configure the server and so I install nullmailer by running the following:

apt-get install nullmailer

Then when the installation prompts for smarthosts I enter into the provided box (substituting <my-api-key> for the API key I got from SendGrid earlier): smtp --port=587 --user=apikey --pass=<my-api-key>

This should be sufficient to get nullmailer to send emails out from your system via sendgrid. At this point I also add a line to the /etc/crontab so that you get an email sent every time the server boots up. That way you’ll get an email if the server reboots unexpectedly.

Please Note. This article is a work in progress and I will be adding further steps at a later time. For now I have some notes for myself that I will be working from:

add to /etc/aliases
call newaliases

configure nullmailer

add '@reboot root echo " booted at $(date)"' to /etc/crontab

apt-get install mariadb-server nginx git

apt-get install screen

echo "set mouse-=a" > ~/.vimrc

echo "[client]" > ~/.my.cnf
echo "user     = root" >> ~/.my.cnf
echo "host     = localhost" >> ~/.my.cnf
echo "password = #########" >> ~/.my.cnf
echo "socket   = /var/run/mysqld/mysqld.sock" >> ~/.my.cnf

2 thoughts on “My custom setup up a server for php, mariadb, nginx”

  1. Thanks for this article. I would be interested in knowing how you got nullmailer to send out emails from your php website signup or form. I am trying to learn and have root access to my laptop I have port forwarded. Am using Hiawatha and Banshee-PHP but can’t get my website to send a registration confirmation email using nullmailer. Have successfully received nullmailer test messages I sent to myself.

    1. Hi Carl,

      Nullmailer is a pretty simplistic mta so using php to send from it is as simple as calling the mail function ( from your script.

      However, email itself is (these days) a complex subject, especially as it relates to sending it publicly – your emails would have to be formatted correctly with the correct headers, not to mention domain and dns configuration, to have a chance of most modern email providers actually delivering you message and not filtering it as spam/junk. Personally, as I mentioned above, I use an external service called sendgrid to send my emails as they help to ensure that such things are dealt with and I don’t have to worry.

      I’ve run email servers in the past and it’s just far more headaches than it’s worth unless you’ve got a lot of time to spend on it.

      I haven’t used Banshee-PHP myself but I know that sendgrid have an api & sdk for php that would allow you to completely bypass using a local mta (nullmailer) for emailing – and this is something I have used. If I were only interested in sending email from php in my setups I wouldn’t even bother with nullmailer – the only reason I do use it is because it enables all programs, and specifically scheduled tasks (using cron), running on a server to email me. If I were building a website for a client I would get them to sign up for a sendgrid account and then use the api directly because it is more reliable and easier to diagnose.

      Now I hope the above makes sense to you. I’d be happy to discuss this further if you want. You could email me at matt at zercat . net

Leave a Reply

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