Background

Installing Apache2 in linux

entry image
#  Basics

# Install Apache
ubuntu
```sudo apt install apache2```
    
# Enable Apache Service
sudo systemctl enable apache2

# View apache version and parameters
## location of configuration file
apachectl -V
httpd -V #Old apache
    
## Full List of apache configuration cmd
$apachectl -t -D DUMP_INCLUDES

# Start Apache
sudo systemctl start apache2

# Restart apache linux
    service apache2 restart
    sudo /etc/init.d/apache2 restart
    
# Configuration path
    /etc/apache2/sites-available/default
    /etc/apache2/httpd.conf
    /etc/apache2/apache2.conf
    /etc/httpd/httpd.conf
    /etc/httpd/conf/httpd.conf

# Log File
    /var/log/apache2/error.log

# Enable mod rewrite
    enable a2enmod rewrite

# View Modules
    sudo apache2ctl -M
    
# Include files from configuration file
    Include "/etc/apache2/conf.d/*.conf"

#default welcome
+ keep this conf before other conf so this can be loaded as default host
+ useful for many virtualhosts
+ open a default page when ip address/misconfigured virtual host is entered
+ dont open another default website
+ or just throw error page

``` xml
#<VirtualHost *:443>
#   DocumentRoot C:/xampp/htdocs/default_welcome/
#   ErrorDocument 404 /index.html
#   
#   SSLEngine on
#    SSLCertificateFile c:/xampp/apache/conf/ssl/example.hostname.com/certificate.crt
#    SSLCertificateKeyFile C:/xampp/apache/conf/ssl/example.hostname.com/private.key   
#</VirtualHost>

#<VirtualHost *:80>
#   ErrorDocument 404 /index.html
#    DocumentRoot C:/xampp/htdocs/default_welcome/  
#</VirtualHost>
```

# Dont allow directory indexing
## conf file
```
Change
    Options Indexes FollowSymLinks
to
    Options FollowSymLinks

    <Directory "{DOCUMENTPATH}/">
        AllowOverride All
        Options FollowSymLinks     
    </Directory>
```

## .htaccess
Options -Indexes

# Dont allow access to folder files
##.htaccess
Deny from all


# .htaccess Redirect
    #301 permanent redir, 302 temporary redirect
    Redirect 301 /main.html /test1/index.html
    ErrorDocument 404 /404.htm
    
    
    #Rewrite Rule
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^empty.html$ http://www.google.com/ [R=301]
        #^empty.html$ Regular expression to redirect
    
    RewriteRule ^(.*)\.html$ $1.php [NC]
        #Rewrite .html to .php
        #[NC] no case upper/lower case
        #[R,NC] temporary redirect
    
    RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]
        #http://example.com/files/games/mygame.zip  changed to 
    
    RewriteRule ^(newspaper|magazine|site)/([^/.]+)/([^/.]+)$ edit.php?category=$1&section=$2&detail=$3
        #http://example.com/newspaper/sports/tennis changed to 
        
    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
        # %{HTTP_HOST} starts with www
        #RewriteRule, will be evaluated as the
        #   same address (parameter %1) and the same page (parameter $1).

# Uses of Mod-Rewrite
    Protect your site from hotlinking and leeching images
    Redirect nonexisting pages to your main site address
    Use multiple domains in one root
    Automatic translation of pages
    Set up cookies and use them later for authentication purposes
    Prevent user agents (bots) from eating bandwidth on your site
    
    
# Deny From ip
	order deny,allow
    # Denies all IP's
    Deny from all
    # This will allow the IP 192.0.2.9
    allow from 192.0.2.9
    # This will allow all IP's from 192.0.2.0 through 192.0.2.255
    allow from 192.0.2

# Enable .htaccess overrides:
    <Directory /var/www/html/public/>
        Options Indexes FollowSymLinks
        AllowOverride All
        #AllowOverride All
        Require all granted
    </Directory>
    
# Ref
https://lowendbox.com/blog/setup-and-configure-mod_rewrite/