Tag Archives: php

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):

127.0.0.1 localhost
127.0.1.1 <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:

127.0.0.1 localhost
<my.static.ip> <hostname> <hostname.fqdn.com>

# 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 <hostname.fqdn.com>.

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.sendgrid.com 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 root:email@address.com to /etc/aliases
call newaliases

configure nullmailer

add '@reboot root echo "server.name 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