Apache2 is the standard Linux web server. It deals with all of the http and https requests sent to the server. Apache2 modules are also used to compile php scripts.
Installing Apache2
These instructions assume you have already setup an AWS instance and have an SSH client (like PuTTY) available.
- Log in to your instance via the SSH client. Transfer to the root user.
- Use YUM to install httpd (the apache2 web server application)
- Press "Y" when it asks if you want to install Apache
- Verify the installation occurred correctly by starting the httpd service
Summary of command line inputs
- $ sudo su
- $ yum install httpd
- .....
- Do you want to install httpd (Y/N): Y
- $ service httpd start
Configuring Apache2
Configuring Apache2 is easiest done with a visual text editor, like included in WinSCP rather than through the command line and vi. You will need to restart the httpd daemon after changing the configuration files in order for the settings to take effect.
Examples settings | ||
---|---|---|
Apache system user | webserv | |
System group | webcln | |
Domain 1 | example.com | |
Domain 1 subdomain | sub.example.com |
Basic Configuration
These settings will need to be changed whether you use a single domain or virtual domains.
- Open the file "/etc/httpd/conf/httpd.conf". Httpd uses the standard C-type commenting, so any line starting with a "#" is commented out and not used in configuring apache2
- Make sure "Listen 80" is uncommented.
- Change "User" to the desired linux user that you want apache to run as. The example user is "webserv"
- Change "Group" to the desired linux user that you want apache to run as. The example group is "webcln"
- Set the "ServerAdmin" to the server admin's email address.
- Add any other index files to "DirectoryIndex" list. Apache will search for the files in order they are listed. Separate multiple file names with spaces.
- Finish the configuration via the Single Domain Configuration OR the Virtual Domains Configuration. I recommend using the Virtual Domains Configuration model, because it easily allows for adding subdomains or redirecting other domains.
Single Domain Configuration
- Open the file "/etc/httpd/conf/httpd.conf" A single domain is setup fully within the core configuration file.
- Uncomment and make the appropriate changes to the following directives.
- Log in to your instance via the SSH client. Transfer to the root user ("sudo su").
- Verify the installation occurred correctly by starting the httpd service.
- Log in to your domain hosting account and change the DNS records to point to the correct IP address.
- ServerAdmin webmaster@example.com
- ServerName www.example.com:80
- ServerAlias www.example.com
- UseCononicalName off
- DocumentRoot "/var/www/html"
- ErrorLog /var/logs/error_log
Virtual Domains Configuration
- Open the directory "/etc/httpd/conf.d/" and create a new file called "vhosts.conf"
- Copy the below configurations and exchange the example values for your server's values. You should leave a copy of the 'default' server at the top of the vhosts file. The first listing of either port (80 for http and 443 for https) will be used when a request does not match any other server name or server alias.
Meaning of each parameter
- NameVirtualHost - Indicated that the particular IP:PORT combination is a virtual host. Need this to instigate the VirtualHost tags later. The value should be structured as IP:PORT. The wildcard "*" can be used to identify any IP address. Port 80 is used for http connections while port 443 is used for https (secure) connections.
- IfModule - Checks to see if a module is installed and usable. Anything within the tags will be processed only if the module indicated in the open tag is installed and usable.
- VirtualHost - This tag identifies a particular virtual host. The contents of the tag must contain the parameters ServerName, and DocumentRoot in order to work. The IP:PORT combination listed in the opening tag must be initiated using the NameVirtualHost parameter.
- ServerName - The name of the webserver, which is normally the web address, in quotes. Apache will be asked for the ServerName by the user's browser. Note: I use the value "default:80" as a catchall for incorrect inquiries to the server. If a user queries your server, on port 80, for ServerName which doesn't exist, the first VirtualHost will be returned as a default. A DNS error can create this situation, but a user can intentionally create this situation. This is possible by directly accessing the server IP address then spoofing the HTTP header with a different web address. You can actually test your own settings this way.
- UseCononicalName - This is a name allocation directive for self-referential URLs. Setting it to 'on' forces Apache to use the hostname and port specified by ServerName where setting it to "off" allows it to first try the hostname and port supplied by the user then use the server values. Setting it to "off" can be a slight security issue, but will generally allow for faster processing of complex situations, especially those involving intranets.
- ServerAdmin - This is the email address of the admin for the particular server, in quotes. This is not essential, but should be included to control the distribution of spam.
- DocumentRoot - This is the directory apache will look for the appropriate web files.
- ErrorLog - This is the error log file to be used for errors occuring with this virtual host.
- SSLEngine - This runs the Apache mod_ssl engine which allows for secure connection and encryption of the information set to the user. You have to use this if you want to use the https protocol.
- SSLVerifyClient - This forces the client to provide the certificate confirmation before receiving any information. This is impractical for most situations, except when using a company intranet. The client must already have the correct certificate in order to authenticate with the server.
- SSLCertificateFile - The location of the ssl certificate file.
- SSLCertificateKeyFile - The location of the ssl certificate key file..
- Create the directories for each virtual account. The example uses the home directory of "/var/www/vhosts" for all of the virtual hosts. Within this directory there is a directory for each domain and within each of those is a directory for the http files (httpdocs), the https files (httpsdocs) and the server files (var). You also need to create a blank "index.html" file in the http and https directories and an error log in the logs directory.
- /var/www/vhosts/example.com/httpdocs/
- /var/www/vhosts/example.com/httpsdocs/
- /var/www/vhosts/example.com/var/logs/
- /var/www/vhosts/example.com/var/certificates/
- Log in to your instance via the SSH client (PuTTY). Transfer to the root user ("sudo su").
- Verify the installation occurred correctly by starting the httpd service ("service httpd start").
- Log in to your domain hosting account and change the DNS records to point to the correct IP address.
Example vhost.conf file
- NameVirtualHost *:80
- <IfModule mode_ssl.c>
- NameVirtualHost *:443
- </IfModule mode_ssl.c>
- <VirtualHost *:80
- ServerName "default:80"
- UseCononicalName off
- ServerAdmin "webmaster@example.com"
- DocumentRoot "/var/www/vhosts/default/httpdocs"
- ErrorLog "/var/www/vhosts/default/var/logs/error_log"
- <IfModule mode_ssl.c>
- SSLEngine off
- </IfModule mode_ssl.c>
- </VirtualHost>
- <IfModule mode_ssl.c>
- <VirtualHost *:443
- ServerName "default:443"
- UseCononicalName off
- ServerAdmin "webmaster@example.com"
- DocumentRoot "/var/www/vhosts/default/httpsdocs"
- ErrorLog "/var/www/vhosts/default/var/logs/error_log"
- SSLEngine on
- SSLVerifyClient none
- SSLCertificateFile/var/www/vhosts/default/var/certificates/default.crt
- SSLCertificateKeyFile /var/www/vhosts/default/var/certificates/default.key
- </VirtualHost>
- </IfModule mode_ssl.c>