Linux as a web server
Tags: centos, devops, webdev, virtualhost
Published: 2019-09-25 04:36:31

#FAQ

> Who is this post for?
>> Those who need some [#eli5](https://lmgtfy.com/?q=eli5&p=1) 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 `a.b.com` and `d.e.com`.

> Where to learn theory of everything?
>> Just Google it with the following keyword: [LPIC](https://www.lpi.org/)

# Connect to your server!

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

# 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 127.0.0.1 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 www.example.com:80` => `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 127.0.0.1`

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

<https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-firewalld-on-centos-7>

# Install PHP

Install and enable EPEL repository:

1- `sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm`

Install and enable Remi repository

2- `sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm`

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:

<https://linuxize.com/post/how-to-setup-ftp-server-with-vsftpd-on-centos-7/>

# 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"
export HISTTIMEFORMAT='%F %T '
```

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 http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm`

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)

# WhatIsMyIP?

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:

`HOSTNAME=myserver.domain.com`

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 mycustomcomputername.com)

You can check each domain's propagation using DNS checkers online. e.g-> <https://dnschecker.org/>
Also there is a geeky way: `nslookup domain.com`

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:

4-

```
zone "mydomain.ir" IN {
    type master;
    file "mydomain.ir.zone";
    allow-transfer { none; };
};

zone "mycustomer1domain.com" IN {
    type master;
    file "customers.zone";
    allow-transfer { none; };
};

zone "mycustomer2domain.com" IN {
    type master;
    file "customers.zone";
    allow-transfer { none; };
};
```

So you have to create the `customers.zone` and `mydomain.ir.zone` files you just mentioned in directory `/var/named`:

5.1- `sudo touch /var/named/customers.zone`
5.2- `sudo touch /var/named/mydomain.ir.zone`

Then edit them as you wish:

6-

```
$TTL 86400
@   IN  SOA     ns1.mydomain.com. root.mydomain.com. (
        2013042201  ;Serial
        3600        ;Refresh
        1800        ;Retry
        604800      ;Expire
        86400       ;Minimum TTL
)
; Specify our two nameservers
		IN	NS		ns1.mydomain.ir.
		IN	NS		ns2.mydomain.ir.
; 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 `ns1.mydomain.ir` and `ns2.mydomain.ir` 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
</VirtualHost>
```


# Refrence:
<https://twitter.com/MRezaTayyebi/status/1152794765164974080>
<https://support.rackspace.com/how-to/centos-6-apache-and-php-install/>
<https://www.tecmint.com/install-php-7-in-centos-7/>
<https://www.digitalocean.com/community/tutorials/how-to-create-a-sudo-user-on-centos-quickstart>
<https://gist.github.com/tayyebi/5e03f83dff1b897a51e530748d74e872>
<https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-centos-7/>
<https://support.rackspace.com/how-to/centos-hostname-change/>
<https://www.digitalocean.com/community/tutorials/how-to-install-the-bind-dns-server-on-centos-6>
<https://httpd.apache.org/docs/2.4/vhosts/examples.html>