Apache

= Intro =

Source: vogella.com


 * Apache remained the most widely used web server software, estimated to serve 46% of all active websites and 43% of the top million websites.
 * The name was widely believed to be a pun on 'A Patchy Server' (since it was a set of software patches).
 * When Apache is running, its process name is sometimes httpd, which is short for "HTTP daemon."

= Basics = apache2 -l
 * List the available modules of the Apache HTTP server

/etc/apache2/apache2.conf
 * Main configuration file for the Apache Http server

/var/log/apache2/error.log grep MaxClients /var/log/apache2/error.log
 * The error log of Apache is located in the

= Multi-Processing-Module = Apache HTTP can run in different modes. MPM modes determine how the web requests of users are answered.

sudo apachectl -V | grep -i mpm
 * The selected mode is compiled into the server and can be checked with:

Configure only the module which your server is using.
 * Configuration for the event mpm is stored in below file:

/etc/apache2/mods-available/mpm_event.conf

= htaccess =


 * One major application of this file is to redirect an URL to other URL’s.
 * You should avoid using .htaccess files completely if you have access to httpd main server config file.
 * Using .htaccess files slows down your Apache http server.


 * The following .htacess file reroutes http://vogella.com to http://www.vogella.com.
 * It also redirect access to a certain webpage (/articles/SpringFramework/article.html) to another webpage via a 301 redirect.
 * The 301 redirect will tell search engines that this side has moved and is the recommended way to move webpages.

RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.vogella\.de$ RewriteRule ^(.*)$ http://www.vogella.com/$1 [L,R=301] redirect 301 /articles/SpringFramework/article.html http://www.vogella.com/tutorials/SpringDependencyInjection/article.html

= Using modules =

Apache Http supports the usage of modules. To enable modules use the a2enmod command a2enmod rewrite

Other modules Supporting php and wordpress

sudo apt-get install libapache2-mod-fcgid sudo apt-get install php5-cgi

Afterwards activate the corresponding modules:

sudo a2enmod fastcgi sudo a2enmod proxy sudo a2enmod rewrite
 * 1) required for wordpress blog

= Gzip compression =


 * To optimize the download time of your webpages you can turn on gzip compression.
 * This requires the Apache module "mod_deflate":

a2enmod deflate /etc/init.d/apache2 restart


 * The compression can be activated in the default configuration file for this module located in /etc/apache2/mods-available/deflate.conf or via the ".htaccess" file.

AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript
 * 1) compress all text & html:

= VirtualHost Examples = Source: apache.org

Listen 80  DocumentRoot "/www/example1" ServerName www.example.com
 * Running several name-based web sites on a single IP address
 * 1) Ensure that Apache listens on port 80

# Other directives here 

 DocumentRoot "/www/example2" ServerName www.example.org

# Other directives here 

The server has two IP addresses. On one (172.20.30.40), we will serve the "main" server, server.example.com and on the other (172.20.30.50), we will serve two or more virtual hosts. Listen 80
 * Name-based hosts on more than one IP address

ServerName server.example.com DocumentRoot "/www/mainserver"
 * 1) This is the "main" server running on 172.20.30.40

 DocumentRoot "/www/example1" ServerName www.example.com

# Other directives here ... 

 DocumentRoot "/www/example2" ServerName www.example.org

# Other directives here ... 

 DocumentRoot "/www/server1" ServerName server.example.com ServerAlias server 
 * Serving the same content on different IP addresses (such as an internal and external address)

Listen 80 Listen 8080
 * Running different sites on different ports

 ServerName www.example.com DocumentRoot "/www/domain-80" 

 ServerName www.example.com DocumentRoot "/www/domain-8080" 

 ServerName www.example.org DocumentRoot "/www/otherdomain-80" 

 ServerName www.example.org DocumentRoot "/www/otherdomain-8080" </VirtualHost>

The server has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example.com and www.example.org respectively. Listen 80
 * IP-based virtual hosting

 DocumentRoot "/www/example1" ServerName www.example.com </VirtualHost>

 DocumentRoot "/www/example2" ServerName www.example.org </VirtualHost>

The server machine has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example.com and www.example.org respectively. In each case, we want to run hosts on ports 80 and 8080. Listen 172.20.30.40:80 Listen 172.20.30.40:8080 Listen 172.20.30.50:80 Listen 172.20.30.50:8080
 * Mixed port-based and ip-based virtual hosts

 DocumentRoot "/www/example1-80" ServerName www.example.com </VirtualHost>

 DocumentRoot "/www/example1-8080" ServerName www.example.com </VirtualHost>

 DocumentRoot "/www/example2-80" ServerName www.example.org </VirtualHost>

 DocumentRoot "/www/example2-8080" ServerName www.example.org </VirtualHost>

Listen 80  DocumentRoot "/www/example1" ServerName www.example.com </VirtualHost>
 * Mixed name-based and IP-based vhosts

 DocumentRoot "/www/example2" ServerName www.example.org </VirtualHost>

 DocumentRoot "/www/example3" ServerName www.example.net </VirtualHost>

 DocumentRoot "/www/example4" ServerName www.example.edu </VirtualHost>
 * 1) IP-based

 DocumentRoot "/www/example5" ServerName www.example.gov </VirtualHost>


 * You enable or disable virtual hosts with the following command.

Enable the vhost sudo a2ensite vogella.conf

Disable the vhost sudo a2dissite www.vogella.com

After enabling sites you have to reload Apache: sudo service apache2 reload

= Custom Logging =

Enabled different logs for multiple virtual hosts  ServerName www.foo.com DocumentRoot /var/www/www.foo.com/htdocs

CustomLog /var/log/apache/www.foo.com-access.log combined ErrorLog /var/log/apache/www.foo.com-error.log </VirtualHost>

 ServerName mail.foo.com DocumentRoot /var/www/mail.foo.com/htdocs

CustomLog /var/log/apache/mail.foo.com-access.log combined ErrorLog /var/log/apache/mail.foo.com-error.log </VirtualHost>

= Using with Flask =

Source: codementor.io

= Troubleshooting =

apachectl -S
 * Debug virtual host configuration

sudo nano /etc/apache2/ports.conf Listen 8080
 * If Apache is not listening to custom port mentioned by Listen directive in conf file from sites-enabled:


 * References