httpd CentOS 7 quickstart

  1. Installation
  2. Subdomains
  3. HTTPS with certbot
  4. Password-Protect a folder

Installation

Install httpd (apache webserver) via sudo yum install httpd
Configuration is at /etc/httpd/conf/httpd.conf
Webpages are located in /var/www/html
If you can't connect check your firewall, maybe it blocks http / port 80 traffic.
A great way to troubleshoot the installation is to use lynx: Install lynx (sudo yum install lynx) and run lynx localhost. Lynx is an in-console web browser. If it doesn't display your website then the installation went wrong. If it displays your website but you can't reach it from outside your machine then your firewall is at fault.

Subdomains

Subdomains are the names infront of your domain name. Example: test.example.com
You can create subdomains via VirtualHosts in your httpd.conf file. To create subdomains you have to point them at your IP via your domain. A good idea would be to point every subdomain (*.example.com) to your Server. Then just add a few VirtualHosts to your config file. Example:


<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:80>
        ServerName test.example.com
        DocumentRoot /var/www/test_html
</VirtualHost>

The first VirtualHost you create will be the default, so if people go to not.example.com they will see the first VirtualHost you wrote. Don't forget to create the test_html folder and put stuff in there.
ServerAlias means you can have multiple subdomains pointing to one VirtualHost. Don't forget to restart your webserver afterwards with sudo systemctl restart httpd.

HTTPS

Having an HTTPS webserver is standard nowadays, and easy to do aswell.
Install certbot to get ssl certificates for https: sudo yum install certbot
To Create these certificates just run the command (in our example for example.com and test.example.com):
sudo certbot --apache -d test.example.com -d example.com
This command will fetch you your certificates for the provided domains. You will get asked a few questions, just fill them out correctly. If certbot asks you if you want auto redirect from HTTP to HTTPS say Yes (the second option) so that traffic automatically runs on HTTPS only.
Afterwards check your firewall if you are allowing https traffic.
Then just restart httpd again and you are ready to go.
My test-site for this: https://ad.shira.at

Password-Protect a folder on your website

Sometimes you have to protect a folder, for example the testing folder, from the public eye. To do this you can password-protect it via simple http authentication.
This way the folder won't show up in an index listing aswell.
To protect a specific URL with a password you simply have to create a password file and add a few lines to the config file.
First run sudo htpasswd -c /etc/httpd/.htpasswd user to create a new password file and create a password for the user 'user'. Afterwards you just have to copy the following line to your /etc/httpd/conf/httpd.conf file and you are ready to go:


<Directory "/var/www/html/myfolder">    
    AuthType Basic
    AuthName "Unauthorized access is prohibited."
    AuthUserFile /etc/httpd/.htpasswd
    Require valid-user
</Directory>

Now your folder needs a user and password before it can be accessed via a browser.