Apache Http Server (httpd)

Apache Server

Apache Http Server

  • Most popular web server on Internet

Install Apache WebServer

# Debian
sudo apt-get update
sudo apt-get install apache2

# Apache Control Commands - Debain
sudo apahectl start | stop | reload

# Apache Directory - Debian
cd /etc/apache2

# Centos
sudo yum install httpd

# Apache Control Commands - Centos
sudo systemctl start | stop | status httpd

# Apache Directory - Centos
cd / etc/httpd

Apache Virtual Host

To run more than one site on a single machine, you need to setup virtual hosts for sites your plan to host on an apache server.

Name Based Virtual Hosts (Most Common)

  • The server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.

IP Based Virtual Hosts

  • IP-based virtual hosting is a method to apply different directives based on the IP address and port a request is received on.
  • Most commonly, this is used to serve different websites on different ports or interfaces.

Setting up Virtual Directories

sudo mkdir -p /var/www/aayushtuladhar.com/public_html
sudo chown -R $USER:$USER /var/www/aayushtuladhar.com/public_html

Creating Virtual Host Files

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/aayushtuladhar.com.conf

<VirtualHost *:80>
    ServerAdmin admin@aayushtuladhar.com
    ServerName aayushtuladhar.com
    ServerAlias aayushtuladhar.com *.aayushtuladhar.com
    DocumentRoot /var/www/aayshtuladhar.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
  • ServerName - Base Domain that should match for the virtual host definition.
  • ServerAlias - Lists names are other names which people can use to see that same web site.

Enable Virtual Host

sudo a2ensite example.com.conf

Disable Default Virtual Host

sudo a2dissite 000-default.conf

Restart Apache

sudo service apache2 restart    # Ubuntu 15.10
sudo systemctl restart apache2  # Ubuntu 14.10 and Earlier

Reverse Proxy using Apache

A proxy server is an intermediary server that forwards requests to different servers. A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server.

Common uses for reverse proxy server:

  • Load Balancing
  • Web Acceleration
  • Security

Using mod_proxy module, Apache Server can be used as reverse proxy to relay HTTP / HTTPS requests to other machines.


<VirtualHost *:80>
    ServerAdmin hello@aayushtuladhar.com
    ServerName digimarketplace-api.aayushtuladhar.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Order deny,allow
        Allow from all
    <Location />

Load Balancing

  • BalanceMember - Declare Balance Members
  • ProxySet - Sets load balancing method
<VirtualHost *:80>
  # Turn off ProxyRequests to avoid any unwanted traffic
  ProxyRequests off

  <Proxy balancer://nodeCluster>
    # NodeAPI1

    Require all granted
    ProxySet lbmethod=byrequests

  ProxyPass /api balancer://nodeCluster