The Perfect Server - Ubuntu 16.04 (Nginx, MySQL, PHP, Postfix, BIND, Dovecot, Pure-FTPD and ISPConfig 3.1)

This tutorial shows the steps to install an Ubuntu 16.04 (Xenial Xerus) server with Nginx, PHP, MariaDB, Postfix, pure-ftpd, BIND, Dovecot and ISPConfig 3.1. ISPConfig is a web hosting control panel that allows you to configure the installed services through a web browser. This setup provides a full hosting server with web, email (inc. spam and antivirus filter), Database, FTP and DNS services.

1. Preliminary Note

In this tutorial, I will use the hostname server1.example.com with the IP address 192.168.1.100 and the gateway 192.168.1.1 for the network configuration. These settings might differ for you, so you have to replace them where appropriate.  Before proceeding further, you need to have a basic minimal installation of Ubuntu 16.04 as explained in tutorial.

The steps in this tutorial have to be executed as root user, so I will not prepend "sudo" in front of the commands. Either Login as root user to your server before you proceed or run:

sudo -s

to become root when you are logged in as a different user on the shell.

The commands to edit files will use the editor "nano", you can replace it with an editor of your choice. Nano is an easy to use file editor for the shell. If you like to use nano and haven't installed it yet, run:

apt-get install nano

2. Update Your Linux Installation

Edit /etc/apt/sources.list. Comment out or remove the installation CD from the file and make sure that the universe and multiverse repositories are enabled. It should look like this:

nano /etc/apt/sources.list

#

# deb cdrom:[Ubuntu-Server 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420)]/ xenial main restricted

#deb cdrom:[Ubuntu-Server 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420)]/ xenial main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://de.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://de.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## universe WILL NOT receive any review or updates from the Ubuntu security
## team.
deb http://de.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial universe
deb http://de.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://de.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://de.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://de.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://de.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

Then run:

apt-get update

To update the apt package database and then:

apt-get upgrade

to install the latest updates (if there are any). If you see that a new kernel gets installed as part of the updates, you should reboot the system afterward:

reboot

 

3. Change the Default Shell

/bin/sh is a symlink to /bin/dash, however we need /bin/bash, not /bin/dash. Therefore we do this:

dpkg-reconfigure dash

Use dash as the default system shell (/bin/sh)? <-- No

If you don't do this, the ISPConfig installation will fail.

 

4. Disable AppArmor

AppArmor is a security extension (similar to SELinux) that should provide extended security. We will cross check if it is installed and remove it if necessary. In my opinion you don't need it to configure a secure system, and it usually causes more problems than advantages (think of it after you have done a week of trouble-shooting because some service wasn't working as expected, and then you find out that everything was ok, only AppArmor was causing the problem). Therefore, I disable it (this is a must if you want to install ISPConfig later on).

We can disable it like this:

service apparmor stop
update-rc.d -f apparmor remove
apt-get remove apparmor apparmor-utils

5. Synchronize the System Clock

It is a good idea to synchronize the system clock with an NTP (network time protocol) server over the Internet. Simply run

apt-get -y install ntp ntpdate

and your system time will always be in sync.

 

6. Install Postfix, Dovecot, MariaDB, phpMyAdmin, rkhunter, Binutils

For installing postfix, we need to ensure that sendmail is not installed and running. To stop and remove sendmail run this command:

service sendmail stop; update-rc.d -f sendmail remove

The error message:

Failed to stop sendmail.service: Unit sendmail.service not loaded.

Is ok, it just means that sendmail was not installed, so there was nothing to be removed.

We can install Postfix, Dovecot, MariaDB (as MySQL replacement), rkhunter, and binutils with a single command:

apt-get -y install postfix postfix-mysql postfix-doc mariadb-client mariadb-server openssl getmail4 rkhunter binutils dovecot-imapd dovecot-pop3d dovecot-mysql dovecot-sieve dovecot-lmtpd sudo

MariaDB is a fork of the MySQL database server, developed by the original MySQL developer Monty Widenius. According to tests found on the internet, MariaDB is faster than MySQL and it's development is going on with more pace, therefore, most Linux Distributions replaced MySQL with MariaDB as default "MySQL alike" database server. In case that you prefer MySQL over MariaDB, replace "mariadb-client mariadb-server" in the above command with "mysql-client mysql-server".

You will be asked the following questions:

General type of mail configuration: <-- Internet Site
System mail name: <-- server1.example.com

Next open the TLS/SSL and submission ports in Postfix:

nano /etc/postfix/master.cf

Uncomment the submission and smtps sections as follows - add the line -o smtpd_client_restrictions=permit_sasl_authenticated,reject to both sections and leave everything thereafter commented:

[...]
submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       -       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o smtpd_reject_unlisted_recipient=no
#  -o smtpd_client_restrictions=$mua_client_restrictions
#  -o smtpd_helo_restrictions=$mua_helo_restrictions
#  -o smtpd_sender_restrictions=$mua_sender_restrictions
#  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
[...]

Restart Postfix afterward:

service postfix restart

We want MariaDB/MySQL to listen on all interfaces, not just localhost. Therefore we edit /etc/mysql/mariadb.conf.d/50-server.cnf (for MariaDB or /etc/mysql/my.cnf (for MySQL) and comment out the line bind-address = 127.0.0.1:

MariaDB

nano /etc/mysql/mariadb.conf.d/50-server.cnf

[...]
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
[...]

Then we restart MariaDB:

service mysql restart

The systemd service name for MariaDB and MySQL is "mysql", so the restart command is the same for both database servers.

Now we set a root password in MariaDB. Run:

mysql_secure_installation

You will be asked these questions:

Enter current password for root (enter for none): <-- press enter
Set root password? [Y/n] <-- y
New password: <-- Enter the new MariaDB root password here
Re-enter new password: <-- Repeat the password
Remove anonymous users? [Y/n] <-- y
Disallow root login remotely? [Y/n] <-- y
Reload privilege tables now? [Y/n] <-- y

MySQL

nano /etc/mysql/my.cnf

[...]
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1
[...]

Then we restart MySQL:

service mysql restart

The systemd service name for MariaDB and MySQL is "mysql", so the restart command is the same for both database servers.

For MySQL and MariaDB:

Now check that networking is enabled. Run:

netstat -tap | grep mysql

The output should look like this:

root@server1:~# netstat -tap | grep mysql
tcp6 0 0 [::]:mysql [::]:* LISTEN 13796/mysqld
root@server1:~#

 

7. Install Amavisd-new, SpamAssassin, And ClamAV

To install amavisd-new, SpamAssassin, and ClamAV, we run

apt-get -y install amavisd-new spamassassin clamav clamav-daemon zoo unzip bzip2 arj nomarch lzop cabextract apt-listchanges libnet-ldap-perl libauthen-sasl-perl clamav-docs daemon libio-string-perl libio-socket-ssl-perl libnet-ident-perl zip libnet-dns-perl postgrey

The ISPConfig 3 setup uses amavisd-new which loads the SpamAssassin filter library internally, so we can stop SpamAssassin to free up RAM:

service spamassassin stop
update-rc.d -f spamassassin remove

Edit the clamd configuration file:

nano /etc/clamav/clamd.conf

and change the line:

AllowSupplementaryGroups false

to:

AllowSupplementaryGroups true 

And save the file.

 To update the ClamAV antivirus signatures and start the Clamd service. The update process can take some time, don't interrupt it.

service clamav-freshclam restart
service clamav-daemon start

7.1 Install Metronome XMPP Server (optional)

The Metronome XMPP Server provides an XMPP chat server. This step is optional, if you do not need a chat server, then you can skip this step. No other ISPConfig functions depend on this software.

Install the following packages with apt.

apt-get -y install git lua5.1 liblua5.1-0-dev lua-filesystem libidn11-dev libssl-dev lua-zlib lua-expat lua-event lua-bitop lua-socket lua-sec luarocks luarocks

luarocks install lpc

Add a shell user for Metronome.

adduser --no-create-home --disabled-login --gecos 'Metronome' metronome

Download Metronome to the /opt directory and compile it.

cd /opt; git clone https://github.com/maranda/metronome.git metronome
cd ./metronome; ./configure --ostype=debian --prefix=/usr
make
make install

Metronome has now be installed to /opt/metronome.

Share this page:

Suggested articles

17 Comment(s)

Add comment

Comments

From: Ionut Daniel at: 2017-02-08 10:54:26

A perfect tutorial

Everything works great but in my system nginx failed to start on reboot so, i have to add this line net.ipv4.ip_nonlocal_bind = 1 into this file /etc/sysctl.conf and now nginx start on system restart.

Just in case someone have same problem.

 

From: Nexusguy59 at: 2017-02-23 12:12:27

Hello Guys,

       I have a question, How would I go about making sure I get alerts via my gmail account. Is there a way to setup the email server part to use gmail to send alerts?

 

Thanks,

Michael Cooper 

From: till at: 2017-02-23 12:19:20

Set the admin email address to your gmail address under system > interface config.

From: Freddie at: 2017-02-23 18:53:04

what code have to be enter here in the nginx Directives for wordpress site:

l amcurrenly have this code 

 

location /phpmyadmin {

               root /usr/share/;

               index index.php index.html index.htm;

               location ~ ^/phpmyadmin/(.+\.php)$ {

                       try_files $uri =404;

                       root /usr/share/;

                       fastcgi_pass unix:/var/run/php5-fpm.sock;

                       fastcgi_param HTTPS on; # <-- add this line

                       fastcgi_index index.php;

                       fastcgi_param SCRIPT_FILENAME $request_filename;

                       include /etc/nginx/fastcgi_params;

                       fastcgi_param PATH_INFO $fastcgi_script_name;

                       fastcgi_buffer_size 128k;

                       fastcgi_buffers 256 4k;

                       fastcgi_busy_buffers_size 256k;

                       fastcgi_temp_file_write_size 256k;

                       fastcgi_intercept_errors on;

               }

               location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

                       root /usr/share/;

               }

        }

        location /phpMyAdmin {

               rewrite ^/* /phpmyadmin last;

 

        }

 

 

From: Gromy at: 2017-03-02 18:05:35

8. Install Nginx, PHP5 (PHP-FPM), and Fcgiwrap

?????? ? ????????, ? ?????? install PHP5, in text - PHP7

From: luis jhoham venegas tobar at: 2017-03-22 22:36:59

Good afternoon, I have done the tutorial but it does not work the ftp always says "The server sent a passive response with a non-routable address. Using the server address instead.", Can help me to solve this thanks

From: JHB at: 2017-03-28 19:28:44

I've followed the tutorial on a fresh ubuntu 16.04 server vm. No errors during build. But the ISP config login screen won't show up on my browser. I keep getting 'This site can't be reached' followed by a line stating that my webserver refused to connect.

doing netstat shows the server is not listening on 8080 or 8081, which are defined sites on nginx by ISPConfig. I can hit the default nginx page just fine so the webserver IS running.

Don't even know where to start troubleshooting as the nginx logs are not showing anything when I tail it as I try to hit the ISPConfig page.

Completely lost at this point on what the hell is going on.

From: till at: 2017-03-29 06:22:56

Please post in the forum to get help with yur issue.

From: Julius at: 2017-04-04 20:18:57

Got a weird error installing pure-ftpd, not much useful to find about it online. Anyone?:

# apt-get install pure-ftpd-common pure-ftpd-mysqlReading package lists... DoneBuilding dependency treeReading state information... Done0 upgraded, 0 newly installed, 2 reinstalled, 0 to remove and 9 not upgraded.Need to get 0 B/259 kB of archives.After this operation, 0 B of additional disk space will be used.Preconfiguring packages ...Can't exec "/tmp/pure-ftpd-common.config.PxMjbV": Permission denied at /usr/share/perl/5.22/IPC/Open3.pm line 178.open2: exec of /tmp/pure-ftpd-common.config.PxMjbV configure 1.0.36-3.2build1 failed at /usr/share/perl5/Debconf/ConfModule.pm line 59.(Reading database ... 135732 files and directories currently installed.)Preparing to unpack .../pure-ftpd-common_1.0.36-3.2build1_all.deb ...Unpacking pure-ftpd-common (1.0.36-3.2build1) over (1.0.36-3.2build1) ...Preparing to unpack .../pure-ftpd-mysql_1.0.36-3.2build1_amd64.deb ...Unpacking pure-ftpd-mysql (1.0.36-3.2build1) over (1.0.36-3.2build1) ...Processing triggers for systemd (229-4ubuntu16) ...Processing triggers for ureadahead (0.100.0-19) ...Setting up pure-ftpd-common (1.0.36-3.2build1) ...Setting up pure-ftpd-mysql (1.0.36-3.2build1) ...

From: Ben at: 2017-04-05 09:54:03

After install completed, with no clients or sites yet, under System > Server Config > Web I'm trying to set the Website basedir to something more logical. Whatever I fill in, it keeps on failing;"1. Invalid website basedir."I'm using the latest ISPconfig 3.1dev from github master on Ubuntu 16.04 with nginx only (no apache, luckily). Please advice, assuming I can't live with the deep sub sub apache structure of /var/www/clients/ etc.

From: till at: 2017-04-05 10:18:25

ISPConfig requires that structure and it creates symlinks for easy shell navigation. I would call a navigation path /var/www/yourdomain.tld/ to access a domain short and logical and that's what ISPConfig is using when you keep the default settings.

From: Joerg at: 2017-04-14 18:25:03

 I configured fail2ban as described and get this error message when I restart the service:

"Job for fail2ban.service failed because the control process exited with error code. See "systemctl status fail2ban.service" and "journalctl -xe" for details."

systemctl status fail2ban.service says this:

"

fail2ban.service - Fail2Ban Service

   Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)

   Active: inactive (dead) (Result: exit-code) since Fr 2017-04-14 20:20:06 CEST; 1min 37s ago

     Docs: man:fail2ban(1)

  Process: 4728 ExecStart=/usr/bin/fail2ban-client -x start (code=exited, status=255)

 

Apr 14 20:20:06 server1.example.com systemd[1]: fail2ban.service: Control process exited, code=exited status=255

Apr 14 20:20:06 server1.example.com systemd[1]: Failed to start Fail2Ban Service.

Apr 14 20:20:06 server1.example.com systemd[1]: fail2ban.service: Unit entered failed state.

Apr 14 20:20:06 server1.example.com systemd[1]: fail2ban.service: Failed with result 'exit-code'.

Apr 14 20:20:06 server1.example.com systemd[1]: fail2ban.service: Service hold-off time over, scheduling restart.

Apr 14 20:20:06 server1.example.com systemd[1]: Stopped Fail2Ban Service.

Apr 14 20:20:06 server1.example.com systemd[1]: fail2ban.service: Start request repeated too quickly.

Apr 14 20:20:06 server1.example.com systemd[1]: Failed to start Fail2Ban Service

"

Whats the problem and how can I solve it?

From: Raziel at: 2017-04-19 23:34:38

Try to start the fail2ban daemon with:# fail2ban-client -x -v start

on the command-line and see the output.

From: Srinivasa varma at: 2017-05-04 10:46:31

Hello,

I have followed the above article and  installed the ISPConfig successfully, but mail sending and receiving is not happening. Is ther any aditional configuration? please help me on this. 

Thanks.

From: till at: 2017-05-04 10:53:21

There is no additional configuration required. Please make a post in the forum with an excerpt of your mail.log file to get help with your problem.

From: Dario at: 2017-06-19 04:53:12

I love this tutorial, and I followed it to the letter minus a simple change -- since I'm on EC2, using RDS instance instead of localhost. Everything went well, however once ISPConfig is installed, I can't access it the way the tutorial says.  I have no clue where it is, there are no vhosts setup, trying to figure it out. Quite frustrating to have everything install, but... to no end.

From: till at: 2017-06-19 07:16:22

Please make a post in the forum so we can help you to get your server fully working :)