How to Set Up an Email Server with Postfix, Dovecot and Roundcube on Ubuntu 18.04

Postfix is a Mail Transfer Agent(Agent). It is a powerful open-source application that is capable of receiving and sending emails.

Dovecot is a free open-source POP3 and IMAP server that delivers and retrieves emails to local mailboxes on the Linux system.

Roundcube is a web-based email client that works pretty well with Postfix and Dovecot.

While utilizing the POP and IMAP protocol on Dovecot, Roundcube can read emails stored by Dovecot on virtual mailboxes.

At the same time, Roundcube can submit emails to Postfix using the SMTP protocol.

So by harnessing the power of three open source applications (Postfix, Dovecot, and Roundcube), you can create a fully functional send/receive email server.

This is a comprehensive guide for setting up an email server with Postfix, Dovecot, and Roundcube on Ubuntu 18.04 server.

Note: For a better experience with Postfix, Sign up with Digital Ocean and get up to $100 free trial credit. We recommend Digital Ocean because they allow outbound and inbound traffic to port 25.

Prerequisites

To follow along with this guide, you will require the following:

  1. A new VPS(Virtual Private Server) account. Sign up with Digital Ocean and enjoy up to $100 worth of free trial credit.
  2. A domain name(e.g. example.com)
  3. A VPS instance running Ubuntu 18.04 as the operating system
  4. A non-root user that can perform sudo tasks

Step 1: Configuring DNS Server

Your email server must have a fully qualified domain name.

To set up this, you must point the A record of your domain to the public IP address associated with your VPS instance.

You must also set MX records on your domain name DNS records editor. This is done from the control panel of your VPS provider.

Then, you need to change the hostname of your server.

To edit the server hostname, open the /etc/hostname file and change the name to something appropriate e.g., mail

$ sudo nano /etc/hostname

Save the file by pressing CTRL+X, Y and, Enter

The next step is editing the hosts’ file /etc/hosts. Open the file using nano text editor.

$ sudo nano /etc/hosts

Make sure you have the below two entries at the top of the file. Replace example.com with your domain name.

127.0.0.1    localhost
127.0.1.1    mail.example.com mail

Reboot the system

$ sudo reboot

Step 2: Installing Apache Web Server

With the server name and DNS records configured, we will go ahead and install Apache web server.

Apache is primarily needed by Roundcube email client for it to run from a browser like Google Chrome.

Install Apache.

$ sudo apt-get update
$ sudo apt-get install apache2

Press Y and hit Enter when prompted to confirm the installation.

Also, make sure the Mod_Rewrite module is enabled. This will be required for Roundcube to work:

$ sudo a2enmod rewrite

Restart Apache.

$ sudo systemctl restart apache2

Step 3: Installing MySQL Database Server

Roundcube requires MySQL for it to work, so we are going to install the database server.

$ sudo apt-get install mysql-server

Press Y and Enter to confirm the installation.

Next, run the command below to secure MySQL database server.

$ sudo mysql_secure_installation

Step 3: Installing Let’s Encrypt Certificate

In order for the mail server to use SSL, we need to install an SSL certificate.

To do this, run the command below and remember to replace example.com with the exact domain name that you intend to use with your mail server.

$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-apache
$ sudo certbot --apache -d example.com -d www.example.com

Step 3: Installing PHP Scripting Language

Roundcube is written in PHP. As such, we need to install PHP together with all associated modules required by Roundcube.

$ sudo apt-get install php libapache2-mod-php php-mysql

Again, press Y when prompted to confirm the installation and hit Enter to continue.

Step 4: Installing Postfix MTA

Next, we are going to install Postfix Mail Transfer Agent (MTA). To do this, run the command below:

$ sudo apt-get install postfix

Press Y and hit Enter when prompted to confirm the installation.

On the next screen, hit TAB then Enter to continue.

You will be prompted to select the mail server configuration type. Choose Internet Site and press TAB and Enter to continue.

On the next screen, you should enter the name of your domain without the ‘www’ part.

So, if your domain name is www.example.com, enter example.com on the system mail name field and hit Tab and Enter to continue.

After a few seconds, Postfix will be successfully installed on your Ubuntu 18.04 server

Step 5: Configuring Postfix

Postfix MTA is a very powerful and secure out-of-the-box. However, it requires a few configurations for it to work with Ubuntu 18.04 and Dovecot.

The main Postfix configuration file is located at /etc/postfix/main.cf.

We will back up this file before editing it by running the command below.

$ sudo mv /etc/postfix/main.cf /etc/postfix/main.cf.bk

This ensures that we can go back to the default settings in case we mess up with the Postfix configuration.

Next, we can create a new Postfix configuration file using nano text editor:

$ sudo nano /etc/postfix/main.cf

Paste the below information on the file and remember to replace example.com with your domain name.

# GENERAL SETTINGS

smtpd_banner = $myhostname ESMTP $mail_name
biff = no
append_dot_mydomain = no
readme_directory = no

# SMTP SETTINGS 

smtp_use_tls=yes
smtp_tls_security_level = may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# SMTPD SETTINGS 

smtpd_use_tls=yes
smtpd_tls_security_level = may
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_tls_cert_file=/etc/letsencrypt/live/example.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/example.com/privkey.pem
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated,  reject_unauth_destination

# SASL SETTINGS

smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

# VIRTUAL MAIL BOX AND LMTP SETTINGS

virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_mailbox_domains = /etc/postfix/virtual_mailbox_domains



# OTHER SETTINGS

myhostname = mail.example.com
myorigin = /etc/mailname
mydestination =  localhost.$mydomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

Once you add the settings above, save and close the file.

Step 6: Creating Virtual Mail Box Domains

Our Postfix configuration file that we created above instructed the mail server to look for virtual mailbox domains from the /etc/postfix/virtual_mailbox_domains file.

We need to add the domains that we intend to use with the mail server on this file. You can add as many domains as you want.

To keep things simple, we are adding the example.com domain.

Open the file:

$ sudo nano /etc/postfix/virtual_mailbox_domains

Then, add the entry below. Please note, the entry MUST be in two parts as shown below.

example.com #domain

Save and close the file when done.

Since Postfix is not configured to read plain text files, we will convert the file to a format that it can understand using the command below:

$ sudo postmap /etc/postfix/virtual_mailbox_domains

Remember, you must run that command each time you edit the /etc/postfix/virtual_mailbox_domains file.

The next step is setting up the Postfix’s master configuration file /etc/postfix/master.cf.

Open the file using nano text editor.

$ sudo nano /etc/postfix/master.cf

Look for the line below.

#submission inet n       -       y       -       -       smtpd

Then, remove the leading # symbol to uncomment it:

submission inet n       -       y       -       -       smtpd

When done, save the file and close it.

Step 7: Installing Dovecot on Ubuntu 18.04

Next, we will install Dovecot and all associated packages necessary for running IMAP, POP and LMTP protocol.

$ sudo apt-get install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd

Step 8: Configuring Dovecot mail_location

In order for Dovecot to communicate with Postfix and our virtual mailbox domains, we need to make a few changes to its configuration files.

We will start off by editing the /etc/dovecot/conf.d/10-mail.conf file

$ sudo nano /etc/dovecot/conf.d/10-mail.conf

Locate the mail_location parameter :

mail_location = mbox:~/mail:INBOX=/var/mail/%u

Then, change its value as shown below :

mail_location = maildir:/var/mail/vhosts/%d/%n

Save and close the file

In a nutshell, we have instructed Dovecot to look for mails on the /var/mail/vhosts directory.

So for Dovecot to work, we need to create a sub-directory for each domain we intend to use with our email server.

Step 9: Creating Dovecot vhosts Directory

Let’s create the vhosts directory first:

$ sudo mkdir /var/mail/vhosts

Then we can go ahead and create the sub-directory for our domain name.

$ sudo mkdir /var/mail/vhosts/example.com

If you have multiple domains, repeat the command above while replacing the last part of the directory name(example.com) with the name of each domain.

Step 10: Creating Dovecot vmail User and Group

The next step is creating a vmail user and a group. We will also assign the user access to the vhosts directories that we created above.

First, let’s create the group:

$ sudo groupadd -g 5000 vmail

Next, we can create a vmail user and the same to the group that we have created above:

$ sudo useradd -r -g vmail -u 5000 vmail -d /var/mail/vhosts -c "virtual mail user"

Then, we need to assign the ownership of the directories to the vmail user.

$ sudo chown -R vmail:vmail /var/mail/vhosts/

Step 11: Enabling Dovecot for Secure POP3 and IMAP Services

Next we are going to edit the /etc/dovecot/conf.d/10-master.conf file and enable IMAPs and POP3 secure services.

First, open the file

$ sudo nano /etc/dovecot/conf.d/10-master.conf

Then, find the entries below.

inet_listener imaps {
    #port = 993
    #ssl = yes
  }

Change them to:

inet_listener imaps {

    port = 993
    ssl = yes
  }

On the same file, locate the below content.

inet_listener pop3s {

    #port = 995
    #ssl = yes

  }

Change the above to:

inet_listener pop3s {

    port = 995
    ssl = yes

  }

Step 12: Setting up Dovecot LMTP service

On the same file, we need to enable the LMTP service.

Find the entries below:

service lmtp {

unix_listener lmtp {

#mode = 0666

}

And change them to:

service lmtp {

unix_listener /var/spool/postfix/private/dovecot-lmtp {

mode = 0600
user = postfix
group = postfix

 }

Don’t close the file yet.

Step 13: Configuring Dovecot Authentication Socket

We need to configure the authentication socket. So, locate:

service  auth {

...

  # Postfix smtp-auth

  #unix_listener /var/spool/postfix/private/auth {

  #  mode = 0666

  #}



}

And change the above entries to:

service auth {

...

#Postfix smtp-auth

unix_listener /var/spool/postfix/private/auth {

mode = 0666

user=postfix

group=postfix

}

...

Save and close the file when done.

Step 14: Setting Up Dovecot Authentication Process

Next, we will set up Dovecot authentication process by editing the /etc/dovecot/conf.d/10-auth.conf file:

$ sudo nano /etc/dovecot/conf.d/10-auth.conf

Find the entry below.

# disable_plaintext_auth = yes

Then, uncomment it by removing the leading pound symbol

disable_plaintext_auth = yes

Also, we have to change the authentication mechanism from plain to plain login.

On the same file, find the below entry.

auth_mechanisms = plain

And change it to:

auth_mechanisms = plain login

We can now set up the users and passwords configurations.

We will use a password file since it is a flexible method for configuring new users.

First,  disable the default Dovecot behavior for authenticating users using system information.

Locate the line:

!include auth-system.conf.ext

And comment it by adding a pound symbol(#) at the beginning :

#!include auth-system.conf.ext

Then, enable password file configuration. Locate the entry below.

#!include auth-passwdfile.conf.ext

Then, change it to:

!include auth-passwdfile.conf.ext

Save and close the file

Next we will edit the /etc/dovecot/conf.d/auth-passwdfile.conf.ext file.

$ sudo nano /etc/dovecot/conf.d/auth-passwdfile.conf.ext

Make sure the file looks as shown below:

passdb {

  driver = passwd-file

  args = scheme=PLAIN username_format=%u /etc/dovecot/dovecot-users

}



userdb {

driver = static

args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n



 # Default fields that can be overridden by passwd-file

 #default_fields = quota_rule=*:storage=1G

 # Override fields from passwd-file

 #override_fields = home=/home/virtual/%u

}

Save and close the file.

Step 15: Creating Dovecot Password File

The next step is creating a password file for each user that we intend to assign an email account.

$ sudo nano /etc/dovecot/dovecot-users

Users must be added using the format user@domainname.com  followed by the password.

Example:

admin@example.com:{plain}Mis25sOpdsio

You can add as many users as you want and finally save and close the file.

In order for Dovecot to work with SSL for security reasons, we will make a few changes to the /etc/dovecot/conf.d/10-ssl.conf file

$ sudo nano /etc/dovecot/conf.d/10-ssl.conf

Change the ssl parameter value from no to required.

Locate:

ssl = no

And change it to:

ssl = required

Step 16: Configuring Dovecot to Use Let’s Encrypt Certificate

The next step is to point the Let’s encrypt certificate files that were generated earlier to Dovecot.

Find the below two lines on the /etc/dovecot/conf.d/10-ssl.conf file:

#ssl_cert = </etc/dovecot/dovecot.pem

#ssl_key = </etc/dovecot/private/dovecot.pem

Then change their values to:

ssl_cert = </etc/letsencrypt/live/example.com/fullchain.pem

ssl_key = </etc/letsencrypt/live/example.com/privkey.pem

Save and close the file and restart Apache, Postfix and Dovecot for the changes to be effected:

$ sudo service apache2 restart 
$ sudo service postfix restart 
$ sudo service dovecot restart

Step 17: Installing Roundcube

The next step is installing Roundcube email client on Ubuntu 18.04 server.

Since the Roundcube package is available on the Ubuntu software repository, we are going to run the command below to install it:

$ sudo apt-get install roundcube

Press Y and hit Enter when prompted to confirm the installation.

Next, add Roundcube path to the default SSL configuration file:

$ sudo nano /etc/apache2/sites-enabled/000-default-le-ssl.conf

Add Alias /mail /usr/share/roundcube below ServerAlias www.example.com

Alias /mail /usr/share/roundcube

Save and close the file. Then, restart Apache for the changes to take effect

$ sudo service apache2 restart

Step 18: Testing the Configuration

To test the configuration, visit www.example.com/mail on your server and replace example.com with your domain name.

You should see a page similar to the one shown below.

Enter the username and password that you created on the Dovecot password file to login. On the server field, enter ‘localhost’.

If you have reached this step, congratulations! From this point forward, you can start sending and receiving emails right from the Roudcube dashboard as shown below.

To check Postfix error log file, run the command below.

$ sudo tail -f /var/log/syslog | grep postfix

You can also telnet Gmail servers to make sure outbound traffic from port 25 is allowed from your VPS provider.

$ telnet alt4.gmail-smtp-in.l.google.com 25

In case the port is blocked, contact your VPS provider and ask them to enable it.

They will be more than happy to assist you provided you won’t send SPAM emails from the server.

Conclusion

In this guide, we have shown you how to configure an email server with Postfix, Dovecot, and Roundcube on Ubuntu 18.04 VPS.

We hope you enjoyed the guide. To test out configuring email with Postfix, Dovecot, and Roundcube, Sign up with Digital Ocean today and enjoy up to $100 worth of free cloud credit!

How to Set Up Nginx Server Blocks on Ubuntu 18.04 Server

Nginx is one of the best free web servers that offer accelerated content and application delivery. The open-source software was built for speed, improved security, and stability for the world’s busiest sites.

Ubuntu 18.04, on the other hand, is a strong and secure open-source operating system that is fully customizable.

With Nginx and Ubuntu 18.04 combined, one can run a very powerful web server. The beauty of Nginx is the ability to host multiple sites on a single Ubuntu 18.04 server (e.g. a VPS machine).

In this guide, we will show you how to set up multiple websites on Ubuntu 18.04 server with Nginx server blocks.

Prerequisites

In order to accomplish the setup, make sure you have the following:

  1. A VPS account. Signup with Digital Ocean and enjoy up to $100 worth of free trial credit.
  2. A VPS instance running Ubuntu 18.04 as the operating system.
  3. A non-root user that can perform sudo tasks on the VPS instance

Step 1: Installing Nginx Web Server on Ubuntu 18.04

The first step is installing the Nginx web server. Before you do this, SSH to your server and update the software information index to ensure we are pulling the latest version from Ubuntu’s software repository.

$ sudo apt-get update

Then, install Nginx.

$ sudo apt-get install nginx

Output:

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following additional packages will be installed:

  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0

  libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip

  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter

  libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common

  nginx-core

Suggested packages:

  libgd-tools fcgiwrap nginx-doc ssl-cert

The following NEW packages will be installed:

  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0

  libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip

  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter

  libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx

  nginx-common nginx-core

0 upgraded, 18 newly installed, 0 to remove and 103 not upgraded.

Need to get 2,423 kB of archives.

After this operation, 7,860 kB of additional disk space will be used.

Do you want to continue? [Y/n]

Press Y when prompted to confirm the installing and hit Enter to continue.

Once Nginx is installed, you can verify the installation by entering the public IP address associated with your VPS machine on a web browser like Google Chrome.

192.0.0.1

You should see the default Nginx web page on Ubuntu 18.04 server as shown below.

Step 2: Creating root Directories for the Websites

By default, Nginx creates one server block with /var/www/html as the root directory. Since we want to host multiple sites on the server, we will have to create a separate directory for each website.

We will be creating directories for example.com and example.net website inside the /var/www/ directory. It is a good practice to create a public_html sub-directory on each website.

To do this, run the commands below:

$ sudo mkdir -p /var/www/example.com/public_html
$ sudo mkdir -p /var/www/example.net/public_html

Remember to create all directories for your websites in this step, the number of websites does not matter provided your server resources can handle the traffic. 

Step 3: Setting the Appropriate File Permissions

If you create directories with the sudo command, they will belong to the root user. However, we want to make sure that the regular users on the Linux system can upload website files.

To do this, we will change the ownership of the website document root directories to the current user.

$ sudo chown -R $USER:$USER /var/www/example.com/public_html
$ sudo chown -R $USER:$USER /var/www/example.net/public_html

To ensure that Nginx will be able to read and execute the files placed on the public_html directories, we need to issue the right permissions.

$ sudo chmod -R 755 /var/www/example.com/public_html
$ sudo chmod -R 755 /var/www/example.net/public_html

Step 4: Creating Sample Nginx Index Files

With the directory setup and permissions in place, we can now go ahead and create a sample index file for each of our website.

We will do this using nano text editor

$ sudo nano /var/www/example.com/index.html

Paste the information below

<html>

<head>

  <title>

  Server Block 1/ Virtual Host 1

  </title>

  </head>

  <body>

  <p>This is Nginx virtual host for example.com website</p>

  </body>

</html>

Then press CTRL+X, Y and Enter to save the file.

Repeat the same procedure for the example.net website.

$ sudo nano /var/www/example.net/index.html

Then, enter the information below on the file.

<html>

 <head>

 <title>

 Server Block 1/ Virtual Host 2

 </title>

 </head>

 <body>

 <p>This is Nginx virtual host for example.net website</p>
 
 </body>

</html>

Save and close the file

Step 5: Creating Nginx Server Blocks on Ubuntu 18.04 for Multiple Websites

Our index pages are ready to be served. However, we haven’t instructed Nginx about the website directories that we have created and the name of our domains. This is where Nginx server blocks or virtual hosts come in to play.

As mentioned a while ago Nginx comes with a default virtual host or server block that serves content from the /var/www. The configuration file of this website is located at /etc/nginx/sites-available/default.

Basically, you need to add an Nginx server block on the /etc/nginx/sites-available/ directory for each website you intend to host with Nginx. However, this configuration file won’t be loaded when Nginx start until a link is created under the /etc/nginx/sites-enabled/ directory.

So to allow Nginx to load our example.com and example.net websites, we need to create a separate configuration file for each website on the /etc/nginx/sites-available/ directory and then create symbolic link on the /etc/nginx/sites-enabled/ directory.

First, create a server block for the example.com website.

$ sudo nano /etc/nginx/sites-available/example.com

Then, enter the information below in the file.

server {

            listen 80;

            listen [::]:80;

            server_name example.com;

            root /var/www/example.com/public_html;

            index index.html;

 

            location / {

                        try_files $uri $uri/ =404;

            }

}

Save and close the file by pressing CTRL+X, Y, and Enter.

We also need to create a new server block for the example.net website

$ sudo nano /etc/nginx/sites-available/example.net

Then, enter the information below in the file.

server {

            listen 80;

            listen [::]:80;

            server_name example.net;

            root /var/www/example.net/public_html;

            index index.html;

 

            location / {

                        try_files $uri $uri/ =404;

            }

}

Save the file by pressing CTRL+X, Y and Enter.

Step 6: Enabling Nginx Server Blocks on Ubuntu 18.04

With the Nginx server blocks in place, we can enable them by running the commands below

$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/example.net /etc/nginx/sites-enabled/

In order for the changes to take effect, restart Nginx web server.

$ sudo systemctl restart nginx

Step 7: Testing Nginx Server Blocks on Ubuntu 18.04

Once everything is in place, you can test your configuration. If you are running Windows on your local machine, you need to add example.com and example.net together with the public IP address associated with your VPS on the hosts’ file.

C:\Windows\System32\drivers\etc\hosts

Open the file and add the below information. Remember to replace the 192.0.0.1 with your VPS public IP address

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
...

192.0.0.1      example.com
192.0.0.1      example.net

Save the file and visit example.com website. If you followed the guide, you should see the below website.

Then, visit example.net website and confirm if you can see the below output.

Congratulations, if you have reached this far. That’s all when it comes to setting up multiple websites on Nginx with Ubuntu 18.04 server

Conclusion

In this guide, we have taken you through the steps of setting up Nginx server blocks on Ubuntu 18.04 VPS in order to run multiple websites from one server instance.

You can extend the server blocks and hosts as many websites as you want provided your server resources can handle the load.

Signup with Digital Ocean today to enjoy up to $100 worth of free trial credit and run multiple websites on their VPS server.

How to Configure Virtual Hosts On Nginx With CentOS 7

Nginx server blocks also known as Nginx virtual hosts are configurations for setting up multiple websites on a single server.

If you have more than one domain but you don’t want to incur additional costs on VPS plans, you may utilize the Nginx server blocks feature.

Although Nginx is a free and an open-source web server, it can handle more concurrent connections than Apache.

in this guide, we will show you how to set up multiple websites on Nginx with CentOs 7 as the operating system.

Prerequisites

Before you begin, make sure you have the following:

Step 1: Installing Nginx on CentOs 7 Server

We will start off by installing Nginx web server. The package is available on the EPEL repository. So, we need to add the information on the server:

$ sudo yum install epel-release

Then, install Nginx:

$ sudo yum install nginx

Press Y and hit Enter when prompted to confirm the installation.

Once the Nginx web server is installed, start it by typing the command below:

$ sudo systemctl start nginx

Then, enable Nginx to start at boot.

$ sudo systemctl enable nginx

Then, allow the HTTP and HTTPS traffic from CentOs firewall.

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-service=https
$ sudo firewall-cmd --reload

You can then confirm if the installation was successful by entering the IP address associated with your server on a browser like Google Chrome.

http://192.0.0.1

You should see the default Nginx home page as shown below:

Step 2: Setting Up Nginx Virtual Host on the First Domain

Next, we are going to configure the first virtual host on Nginx. We will use the example.com as the first domain.

The default Nginx configuration file can be located at /etc/nginx/nginx.conf

Also, the root of the default Nginx website points to  /usr/share/nginx/html.

Since the above directory setup can be a little bit confusing for hosting multiple websites on Nginx, we will create a structure which is more understandable.

First, let’s create the directory to hold our example.com websites data.

$ sudo mkdir -p  /var/www/example.com/public_html

The -p option tells Linux to create any parent directory on the way.

The directory we created above is owned by the root user. However, we want our regular user to be able to modify files on it, so let’s change the directory ownership.

$ sudo chown -R $USER:$USER /var/www/example.com/public_html

Next, we need to set up the appropriate file permissions to allow Nginx to read and execute the files from the public_html directory.

$ sudo chmod -R 755 /var/www/example.com/public_html

Once the directory structure is in place we can create a sample web page on the root of the example.com directory

$ sudo nano /var/www/example.com/public_html/index.html

Then, paste the information below on the file

<html>
  <head>
    <title>Sample web page on example.com website</title>
  </head>
  <body>
    <h1>Nginx server block 1</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.com
  </body>
</html>

Save and close the file by pressing CTRL+X, Y, and Enter

Next, we need to create a server block for the example.com website.

All Nginx virtual host files must end with .conf

The server block configuration files must be placed in the /etc/nginx/conf.d directory.

So let’s create the first Nginx block for the example.com website using nano text editor.

$ sudo nano /etc/nginx/conf.d/example.com.conf

Then, enter the information below on the file

server {
    listen  80;
    server_name example.com;

    location / {
        root  /var/www/example.com/public_html;
        index  index.html index.htm;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Step 3: Setting Up Nginx Virtual Host on the Second Domain

Our server block for the first host is set up. Next, we can go ahead and configure the second virtual host for example.net domain.

First, we create the directory structure.

$ sudo mkdir -p /var/www/example.net/public_html

Then, change the directory ownership.

$ sudo chown -R $USER:$USER /var/www/example.net/public_html

Next, let’s create a sample web page on the example.net root directory.

$ sudo nano /var/www/example.net/public_html/index.html

We can now add the information below to render the HTML web page

<html>
  <head>
    <title>Sample web page on example.net website</title>
  </head>
  <body>
    <h1>Nginx server block 2</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.net
  </body>
</html>

Save and close the file.

We can now go ahead and create a server block for the example.net website.

$ sudo nano /etc/nginx/conf.d/example.net.conf

Then, enter the information below on the file

server {
    listen  80;
    server_name example.net;

    location / {
        root  /var/www/example.net/public_html;
        index  index.html index.htm;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Restart the Nginx web server for the configurations to take effect:

$ sudo systemctl restart nginx

Step 4: Testing Nginx Server Blocks Configuration

To test the configuration add example.com and example.net on your hosts file together with the associated IP address of your VPS machine.

If you are on Windows, locate the file from C:\Windows\System32\drivers\etc\hosts

Then, add the two virtual hosts information at the end as show below.

# Copyright (c) 1993-2009 Microsoft Corp.
...
#	127.0.0.1       localhost
#	::1             localhost

..

149.28.188.94      example.com
149.28.188.94      example.net

Then, example.com on a web browser. You should see a page similar to the one shown below.

You can also visit example.net website which should render the below web page.

If you have reached this far, congratulations! You just learnt how to host multiple websites on Nginx web server running on Centos 7 operating system.

Conclusion

In this guide, we have shown you how to configure multiple websites on CentOs 7 with Nginx using server blocks or virtual hosts.

Sign up with Digital Ocean today and get $100 to host as many websites as you want on one VPS machine.

How to Setup Linux, Nginx, MariaDB and PHP (LEMP) Stack on CentOS 7

LEMP stack is a collection of four open-source applications that are installed together on a server to run dynamic websites. These include Linux, Nginx, MariaDB, and PHP.

When setting up a VPS (Virtual Private Server) account, you will be prompted to select a Linux image (e.g. CentOS 7) when deploying the server. Linux is an operating system based on the Linux Kernel. It is very powerful, secure and runs most of the world’s servers.

Nginx is an event-driven web server popularly used for serving dynamic content. It has a very predictable performance, especially for high trafficked websites.

MariaDB is a fork of MySQL database and is compatible with all SQL commands. It is considered as a drop-in replacement for MySQL. The Relational Database Management System is fast, highly scalable and secure.

PHP is a scripting language that is popularly used as the middleware between the database server and web server.

In this guide, we will show you how to install Nginx, MariaDB, and PHP on a VPS plan running CentOS 7 as the operating system.

Prerequisites

Before you begin make sure you have the following:

Step 1: Installing Nginx on CentOS 7

Nginx packages are available in the EPEL repositories. If you don’t have EPEL repository already installed you can do it by typing:

Nginx packages can be pulled from the EPEL repositories, so you can first install it by typing the command below:

$ sudo yum install epel-release

Once installed, run the command below to install Nginx

$ sudo yum install nginx

Press Y and hit Enter when prompted to confirm the installation. Next, enable the Nginx server by typing the command below:

$ sudo systemctl start nginx

In order to start the web server when the system is rebooted, run the command below:

$ sudo systemctl enable nginx

The default CentOS 7 inbuilt firewall is set to block Nginx traffic. In order to allow inbound traffic to the Nginx server, run the commands below:

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-service=https
$ sudo firewall-cmd --reload

To check if Nginx was successfully installed on your server, enter the IP address associated with your VPS instance on a web browser like Google Chrome:

$ 192.0.0.1

You should see the default Nginx home page as shown below:

Alternatively, if you wish to check the status of the web server on the command line tool, enter the command below:

$ sudo systemctl status nginx

Output:

nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset                                             : disabled)

Active: active (running) since Wed 2019-02-13 07:52:14 UTC; 3min 17s ago

Main PID: 12740 (nginx)

CGroup: /system.slice/nginx.service

├─12740 nginx: master process /usr/sbin/nginx

└─12741 nginx: worker process



Feb 13 07:52:14 lemp-server-1 systemd[1]: Starting The nginx HTTP and revers....

Feb 13 07:52:14 lemp-server-1 nginx[12735]: nginx: the configuration file /e...k

Feb 13 07:52:14 lemp-server-1 nginx[12735]: nginx: configuration file /etc/n...l

Feb 13 07:52:14 lemp-server-1 systemd[1]: Failed to read PID from file /run/...t

Feb 13 07:52:14 lemp-server-1 systemd[1]: Started The nginx HTTP and reverse....

Hint: Some lines were ellipsized, use -l to show in full.

Step 2: Installing MariaDB on CentOS 7

Next, we are going to install the MariaDB database server on the CentOS 7 machine. We are going to use the yum package manager

$ sudo yum install mariadb-server

Press Y and hit Enter when prompted to confirm the installation. After a while, your MariaDB server installation should be completed.

To start the database server, run the command below:

$ sudo systemctl start mariadb

Just like we did for the Nginx server, we want to make sure that the database server is started when the system boots. To do this, run the command below

$ sudo systemctl enable mariadb

You can check if the MariaDB server is running by typing the command below

$ sudo systemctl status mariadb

Output:

mariadb.service - MariaDB database server

Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)

Active: active (running) since Wed 2019-02-13 08:02:03 UTC; 56s ago

Main PID: 12913 (mysqld_safe)

CGroup: /system.slice/mariadb.service

├─12913 /bin/sh /usr/bin/mysqld_safe --basedir=/usr

└─13075 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-...



Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: MySQL manual for more instructions.

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: Please report any problems at http://mariadb.org/jira

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: The latest information about MariaDB is available at http...rg/.

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: You can find additional information about the MySQL part at:

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: http://dev.mysql.com

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: Consider joining MariaDB's strong and vibrant community:

Feb 13 08:02:01 lemp-server-1 mariadb-prepare-db-dir[12835]: https://mariadb.org/get-involved/

Feb 13 08:02:01 lemp-server-1 mysqld_safe[12913]: 190213 08:02:01 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.

Feb 13 08:02:01 lemp-server-1 mysqld_safe[12913]: 190213 08:02:01 mysqld_safe Starting mysqld daemon with databases f...mysql

Feb 13 08:02:03 lemp-server-1 systemd[1]: Started MariaDB database server.

The default MariaDB installation is not secure, so we are going to run the command below to set a root password, remove anonymous users and disable remote access

The default MariaDB installation is not secure, so we are going to run the command below to set a root password, remove anonymous users and disable remote access

$ sudo mysql_secure_installation
Enter current password for root (enter for none):
Set root password: Y
New password: STRONGPASSWORDHERE
Re-enter new password: REPEATSTRONGPASSWORDHERE
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y

Output:

Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

Once the MariaDB  installation is complete, you can log in to the database server using the command below:

$ sudo mysql -uroot -p

Enter the root password of your MariaDB server and press Enter to continue.

You should see the MariaDB command line interface

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Once logged in, you can run any SQL command e.g., to list databases, run the command below:

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

Step 3: Installing PHP on CentOS 7

Next, we are going to install PHP(Hypertext Preprocessor).

This is a general-purpose scripting language that is highly suitable for web applications. In fact, most Content Management Systems (CMS) are coded in PHP.

To install the PHP software package, run the command below:

$ sudo yum install php php-mysql php-fpm

Again, press Y and hit Enter when prompted to confirm the PHP installation on CentOS 7 server.

Once PHP is installed, we will make a few changes to the configuration file. Open the file using nano text editor

$ sudo nano /etc/php.ini

Then, look for the line below:

;cgi.fix_pathinfo=1

And change it to:

cgi.fix_pathinfo=0

Press CTRL+X, Y and hit Enter to continue.

Then, we are going to edit the PHP-fpm configuration file:

$ sudo nano /etc/php-fpm.d/www.conf

Make the following changes to the file:

listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx

We can now start the PHP package by running the command below:

$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm

To enable Nginx to process PHP pages, we are going to create a configuration file using nano text editor

$ sudo nano /etc/nginx/conf.d/default.conf

Then, enter the following details:

server {
    listen   80;
    server_name  192.0.0.1;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file by pressing CTRL+X, Y, and Enter. Then, restart the Nginx web server:

$ sudo systemctl restart nginx

To test if PHP is working, create PHP info file:

$ sudo nano sudo nano /usr/share/nginx/html/info.php

Then enter the information below:

<?php
 phpinfo();
?>

Save and close the file. Then, on a web browser visit the page below and remember to replace 192.0.0.1 with your IP address:

http://192.0.0.1/info.php

You should see a page similar to the one shown below with lots of information about PHP

Conclusion

That’s all when it comes to installing the LEMP stack on your VPS machine. Once the setup is complete, you can import your website files and have a fully functioning web server.

Remember to point your domain name DNS records the public IP address associated with your VPS machine.

New to VPS hosting and cloud computing. Sign up with Digitial Ocean today and enjoy up to $100 worth of free trial credit.

How to Install MySQL 8.0 Community Edition on Ubuntu 18.04

As of February 2019, MySQL 8.0 was the newest version of MySQL. The new and exciting version has great improvements that you probably don’t want to miss.

Being an open source database, MySQL 8.0 has been re-engineered to offer most Windows SQL  functions as well as extended JSON functions. The InnoDB engine has been improved to offer a crash-safe environment and the performance schema has been enhanced.

Another great feature that ships with MySQL 8.0 is role management for organizing users better.

You can take advantage of all these great MySQL 8.0 features by installing it on your Virtual Private Server(VPS).

This guide takes you through the steps of setting up MySQL 8.0 community edition on your Ubuntu 18.04 server.

Prerequisites

Before you begin, make sure you have the following:

  1. A VPS account. Signup with Digital Ocean and get free $100 credit to test this article and other cloud services.
  2. A non-root user that can perform sudo tasks on your Ubuntu 18.04 server.

Step 1: Downloading MySQL 8.0 Repository

The default MySQL edition available on the Ubuntu software repository is not the newest version. So we are going to manually download the repository using wget command.

First,  cd to the tmp directory:

$ cd /tmp

Then, add the repository.

$ wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

Next, we are going to use the dpkg command to install the repository on Ubuntu 18.04 server:

$ sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb

A dialog box will appear, choose OK and press Enter to continue.

Step 2: Installing MySQL 8.0 On Ubuntu 18.04 VPS

Next, update the package information index:

$ sudo apt-get update

To install MySQL, run the command below:

$ sudo apt-get install  mysql-server

Press Y and hit Enter when prompted to confirm the installation.

Towards the end of the installation, you will be prompted to enter the root user of your MySQL server; don’t confuse this with the root account of your VPS server.

 

Enter a strong password and hit Enter to continue. You will be prompted to repeat the password.

Select OK on the next dialog box and hit Enter

Then, select the right authentication plugin

The installation should be successfully installed.  You can confirm the MySQL version on your Ubuntu 18.04 server by running the command below:

$ mysql --version

Output:

mysql  Ver 8.0.15 for Linux on x86_64 (MySQL Community Server - GPL)

Step 3: Loggin to the MySQL 8.0 Server

To log in to the MySQL 8.0 server, run the command below:

$mysql -uroot -p

Enter the root password of the MySQL server and hit Enter to continue.

You will get a prompt similar to the one shown below:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

You can then start issuing commands on the MySQL command line interface e.g., show databases;

mysql> show databases;

Output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec

Conclusion

In this article, we have taken you through the steps of configuring MySQL 8.0 on your Ubuntu 18.04 VPS.

Sign up with Digital Ocean VPS today and get free $100 to deploy MySQL 8.0  on your next project to take advantage of improved features that you can’t find on other versions of MySQL.

How to Set up Multiple Websites on Ubuntu 18.04 VPS with Apache

Apache is one of the best open-source web servers that run millions of sites on the web. It is very fast, secure, flexible, and takes minutes to set up on a Linux server e.g., Ubuntu 18.04.

If you have purchased a VPS plan, there are high chances that you want to run multiple websites on your server. This will reduce additional costs which you would have incurred to deploy additional VPS machines.

Running multiple websites is possible on a single Ubuntu 18.04 server because Apache comes with a feature known as Virtual hosts that allows you to configure unlimited websites on a single server.

In this guide, we will take you through the steps of configuring multiple websites on your Ubuntu 18.04 with Apache.

Prerequisites

To follow along with the guide, you will require the following:

  1. A VPS account. Sign up with Digital Ocean today and get up to $100 worth of free credit to test this article and other cloud products.
  2. A non-root user account that can perform sudo tasks on your VPS server.

For better clarification, we will set up two domains: example.com and example.net on the server.

Step 1: Installing Apache Web Server

The first step is installing the web server. We will use Ubuntu apt manager to install Apache.

First,  update the package information index:

$ sudo apt-get update

Then, install Apache

$ sudo apt-get install apache2

Press Y and hit Enter when prompted to confirm the installation.

Once installed, you can enter the IP address associated with your VPS machine on a web browser like Google Chrome. If the Apache installation was completed successfully, you should see a page similar to the one shown below:

Step 2: Creating the First Virtual Host on Ubuntu 18.04 Server

Apache creates a virtual host when installed for the first time. The configuration file can be located at /etc/apache2/sites-available/000-default.conf. To avoid any conflicts, we need to  disable the virtual host by running the command below.

$ sudo a2dissite 000-default.conf

Next, create a virtual host configuration for the example.com domain.

$ sudo mkdir -p /var/www/example.com/public_html

Next,  we will change the ownership of the directory that we have created above.

$ sudo chown -R $USER:$USER /var/www/example.com/public_html

Then,  issue the right file permissions to the directory.

$ sudo chmod -R 755 /var/www/example.com/public_html

Also, to ensure that newly created files and directories inherit the permissions we  created above, we are going to run the command below:

$ sudo find /var/www/example.com/public_html -type d -exec chmod g+s {} \;

Once we have the directory for the example.com website in place, we can create a new virtual host configuration file and reference to that directory:

$ sudo nano /etc/apache2/sites-available/example.com.conf

Paste the information below:

<VirtualHost *:80>

    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/example.com/public_html>

Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All          
Require all granted

</Directory>

</VirtualHost>

Next, enable the example.com virtual host by running the command below.

$ sudo a2ensite example.com.conf

We can create a sample home page for the example.com website. So, create the file using nano text editor:

$ sudo nano /var/www/example.com/public_html/index.html

Paste the information below in the file.

<html>
  <head>
    <title>Site 1</title>
  </head>
  <body>
    <h1>This is a sample page for example.com website</h1>
  </body>
</html>

Step 3: Creating the Second Virtual Host on Ubuntu 18.04 Server

Just like we have done for the example.com website, we need to create a directory for the example.net virtual host on Apache.

$ sudo mkdir -p /var/www/example.net/public_html

Then we need to change the file ownership and associate them to the currently logged in user.

$ sudo chown -R $USER:$USER /var/www/example.net/public_html

Then, run the command below to issue the right permissions.

$ sudo chmod -R 755 /var/www/example.net/public_html

Make sure that files created under the directory inherit permissions from the parent directory.

$ sudo find /var/www/example.net/public_html -type d -exec chmod g+s {} \;

Create a virtual host file for the example.net website.

$ sudo nano /etc/apache2/sites-available/example.net.conf

Then, paste the information below.

<VirtualHost *:80>
    ServerAdmin admin@example.net
    ServerName example.net
    ServerAlias www.example.net
    DocumentRoot /var/www/example.net/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/example.net/public_html>

Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All           
Require all granted

</Directory>

</VirtualHost>

Close and save the file by pressing CTRL+X, Y and hit Enter.

Next, enable the example.net virtual host by running the command below:

$ sudo a2ensite example.net.conf

Next, create a sample home page for the example.net website.

$ sudo nano /var/www/example.net/public_html/index.html

Enter the information below;

<html>

  <head>

    <title>Site 2</title>

  </head>

  <body>

    <h1>This is a sample page for example.net website</h1>

  </body>

</html>

Press CTRL+X, Y and Enter to save the file.

Step 3: Restart Apache and Test the Configuration

In order for Apache to load the settings for the newly created virtual hosts, it must be restarted.

$ sudo systemctl restart apache2

Then, on your local computer, add the example.com and example.net domain names on the hosts file and point them to the public IP address of your VPS machine and save the file.

If you are running Windows, edit the file c:\Windows\System32\Drivers\etc\hosts and save it. Remember to replace 192.88.99.0 with the IP address associated with your VPS machine:

# Copyright (c) 1993-2009 Microsoft Corp.

...

# localhost name resolution is handled within DNS itself.

#             127.0.0.1       localhost

#             ::1             localhost

192.88.99.1 example.net

192.88.99.1 example.com

..

Then, save the file and visit the example.com on your browser. You should see a page similar to the one below:

Next, visit example.net on your browser to test the 2nd virtual host. Your browser should display the page shown below.

Conclusion

That’s all when it comes to hosting multiple sites on your Ubuntu 16.04 server with Apache. A virtual host is a great feature that you can leverage to reduce cost of ownership when you wish to run multiple sites on a single VPS machine.

Remember, you can configure as many websites as you want provided your VPS plan can handle the disk space, memory, and bandwidth.

The virtual hosts configuration should work pretty well on any Digital Ocean server.  Sign up with Digitial Ocean today and get $100 worth of free trial credit.