arrow_back
Back

Apache HTTP Server on Linux: install, config, and virtual hosts

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

Installation (CentOS)

yum install httpd

Start and check status:

systemctl start httpd
systemctl status httpd

Apache is enabled at boot by default. To change that:

systemctl disable httpd   # do not start on boot
systemctl enable httpd    # start on boot

Configuration (CentOS)

Main config file:

/etc/httpd/conf/httpd.conf

Allow .htaccess overrides under the web root:

<Directory "/var/www">
    AllowOverride All
</Directory>

Virtual hosts (CentOS)

Create site directories:

mkdir -p /var/www/example.com/website
mkdir -p /var/www/example.com/logs
chown -R $USER:$USER /var/www/example.com/website
chmod -R 755 /var/www

Sample index.html:

<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>
    <h1>Success! The example.com virtual host is working!</h1>
  </body>
</html>

Enable per-site configs:

mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

Add to the end of /etc/httpd/conf/httpd.conf:

IncludeOptional sites-enabled/*.conf

Create /etc/httpd/sites-available/example.com.conf:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/website
    ErrorLog /var/www/example.com/logs/error.log
    CustomLog /var/www/example.com/logs/requests.log combined
</VirtualHost>

Enable the site:

ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf

.htaccess tips

If index.php is not found, check RewriteBase — when the app is not in the document root, RewriteBase must match the subdirectory path.

Redirect HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Allow a single IP

Order Allow,Deny
Allow from 123.456.789.123

If traffic goes through Cloudflare, use Cloudflare-aware rules instead of raw client IP checks.

code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward