DEV Community


Posted on

Linux as a web server


Who is this post for?

Those who need some #eli5 step by step notes, to simply configure a web server, to host multiple websites on a single server.

Is this everything about configuring of a web server?

No it's just a tranquilizer to newbie programmers mental challenges on hosting, and rapid Wordpressers who wanna mine money faster!

Spoil it!

Finally you will install Apache, MySQL, BIND, PHP, and etc... on a Linux server and configure it to host multiple domains like and

Where to learn theory of everything?

Just Google it with the following keyword: LPIC

Connect to your server!

use ssh <IP> or ssh <Domain> command to connect to your server.
(e.g-> ssh

Install Apache

What is Apache? Apache serves your content to the world. And also it can give you a hand to create dynamic website with PHP which Wordpress is also a PHP thing.

To install Apache

1- sudo yum install httpd mod_ssl

2- sudo /usr/sbin/apachectl start

If message appears: Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using for ServerName, edit the configuration file using vim or any editor you wish (Step 3).

3- sudo vim /etc/httpd/conf/httpd.conf

and bring the following line out of comment:

4- #ServerName => ServerName MyServerName (which MyServerName is your own server name!)

Allow Apache in firewall. As you know, Apache listens on port HTTP 80 by default.

5.1- sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

5.2- sudo service iptables save

Restart Apache:

6- sudo /usr/sbin/apachectl restart

Test your installation:

7- curl

As you know, it's very critical to allow Apache to access your codes!

8- sudo chcon -t httpd_sys_rw_content_t /var/www/html/mysite -R

Learn more about FirewallD

Install PHP

Install and enable EPEL repository:

1- sudo yum install

Install and enable Remi repository

2- sudo yum install

Install yum-config-manager

3- yum install yum-utils

Install PHP modules which programmers use to reduce their headache

4- yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo

Test the PHP's command line interface (CLI)

5- php -v

Wanna create a secondary sysadmin user? (Sudoer guy)

Create a user

1- ‍‍‍a‍dduser theusername (you say theusername)

Choose a password

2- passwd theusername (you said theusername)

Just like school, anyone is in a group. So you have to add new user to wheel, which is a super user level group:

3- usermod -aG wheel theusername

Switch user

4- su - username

Disable a user login (EMail or FTP accounts maybe)

1- Edit the /etc/passwd

2- Change shell to /bin/nologin

Learn more about configuring FTP server:

Hey server! KEEP EVERYTHING IN MIND; even "Clean your mind"

Edit ~/.bashrc

1- vim ~/.bashrc

2- Put following lines at the end of the file:

export HISTCONTROL=ignoredups:erasedups  
shopt -s histappend
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

3- Test it: history

How to install MySQL

Install wget which is more handy tool than curl for heavy downloads.

1- yum install wget

Download the package:

2- wget

Add the package:

3- sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

Update package manager:

4- yum update

Install the package:

5- sudo yum install mysql-server

Start daemon:

6- sudo systemctl start mysqld

Configure installation:

7- sudo mysql_secure_installation

Test installation:

8- mysql -u root -p (which root is the root username)

Create a MySQL user:

9- mysql> create user 'testuser'@'localhost' identified by 'mysecretpass'; (username is testuser and the password is mysecretpass)

Grant permissions to user:

10- mysql> grant all on databasename.* to 'testuser' identified by 'mysecretpass'; (replace databasename with the database name)


If ifconfig doesn't work:

use: ip addr show

Change hostname

Configuration file is located in /etc/sysconfig/network. So open it in editor and find the related line:

Also you can change the host that is associated with the main IP address for your server in /etc/hosts file.

Test command: hostname

Carefully restart networking (You may lose your connection to remote server!): /etc/init.d/network restart

BIND DNS (Domain Name Server: The magic behind

You can check each domain's propagation using DNS checkers online. e.g->
Also there is a geeky way: nslookup

Install BIND

1- sudo yum install bind bind-utils -y

Configure BIND (Remember that you can define your zones here, but it's better to keep the standards.

2- sudo cat /etc/named.conf

Look for included files, and edit the file as described below:

3- sudo vi /etc/named.rfc1912.zones

Define your zones in file same as following lines:


zone "" IN {
    type master;
    file "";
    allow-transfer { none; };

zone "" IN {
    type master;
    file "";
    allow-transfer { none; };

zone "" IN {
    type master;
    file "";
    allow-transfer { none; };

So you have to create the and files you just mentioned in directory /var/named:

5.1- sudo touch /var/named/
5.2- sudo touch /var/named/

Then edit them as you wish:


$TTL 86400
@   IN  SOA (
        2013042201  ;Serial
        3600        ;Refresh
        1800        ;Retry
        604800      ;Expire
        86400       ;Minimum TTL
; Specify our two nameservers
        IN  NS
        IN  NS
; Resolve nameserver hostnames to IP, replace with your two droplet IP addresses.
ns1     IN  A       SERVER_IP_HERE
ns2     IN  A       SERVER_IP_HERE

; Define hostname -> IP pairs which you wish to resolve
@       IN  A       SERVER_IP_HERE
www     IN  A       SERVER_IP_HERE

Then any domain that is decided to be hosted on this server, must set and as their name server in domain control panel. If website was defined as a Virtual Host in Apache, it will be accessible.

Virtual Hosts

Edit the config file: sudo vi /etc/httpd/conf/httpd.conf foreach website you want to host.

<VirtualHost *:80>
   DocumentRoot "/var/www/html/domainname"
   ServerName domainname
   ServerAlias www.domainname
   RedirectPermanent / https://domainname2


Discussion (0)