From Fedora Project Wiki

(substantially overhaul the page for newer tools (/etc/pki/tls/certs/Makefile) and conventions (webapp access control), add a lot of new information)
Line 1: Line 1:
The Apache HTTP Server is the main web server worldwide
The Apache HTTP Server is one of the most commonly-used web servers. This page acts as a quick start guide to deploying and configuring Apache on Fedora. For (many) more details, please see [https://httpd.apache.org/docs/current/ upstream's extensive documentation].


== Installation ==
== Installation ==
<pre>$ su root
$ yum install httpd</pre>


If you want SSL support install also openssl and mod_ssl
$ su
<pre>$ yum install openssl mod_ssl</pre>
# yum install httpd


Enable start on boot
If you want TLS/SSL support, you can also install {{package|mod_ssl}}, which is based on [https://www.openssl.org OpenSSL]. Alternatives are {{package|mod_gnutls}} (uses [https://www.gnutls.org/ GnuTLS]) and {{package|mod_nss}} (uses [https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS NSS]).
<pre>$ systemctl enable httpd</pre>


=== Create self signed certificate ===
# yum install mod_ssl


Install crypto-utils
To have the server start at each boot:
<pre>$ yum install crypto-utils</pre>


Use genkey to create a certificate for a new hostname
# systemctl enable httpd
<pre>$ genkey hostname</pre>


If your server already has a valid certificate and you want to replace use this to set a different serial number so that clients notice the change in certificate and update to the new one without failing
At this point, you should be able to browse to http://localhost on the server and access the Apache test page. You will most likely not be able to access the server from any other host, yet: we will change this [[#firewall-configuration|later]].
<pre>$ openssl req -x509 -new -set_serial number -key hostname.key -out hostname.crt</pre>


=== Install a certificate ===
=== Create a self signed TLS/SSL certificate ===


If your certificate was generated in another computer move the certificate and the key file to the correct folder
If you want to use TLS/SSL, you will need a server certificate. The simplest choice is to create a 'self-signed' certificate; this does not require you to deal with a certificate signing authority, but means no system will trust your server by default. You may publish and/or note down the key's fingerprint and verify it each time you connect to your server.
<pre>$ mv key_file.key /etc/pki/tls/private/hostname.key
$ mv certificate.crt /etc/pki/tls/certs/hostname.crt</pre>


==== Modify default settings ====
# cd /etc/pki/tls/certs
If you want the default VirtualHost to use a certificate, open /etc/httpd/conf.d/ssl.conf and edit this lines
# make testcert
<pre>SSLCertificateFile /etc/pki/tls/certs/hostname.crt
SSLCertificateKeyFile /etc/pki/tls/private/hostname.key</pre>


==== Settings for individual virtual hosts====
If your server already has a valid certificate and you want to replace it, pass SERIAL= to set a different serial number so that clients notice the change in certificate and update to the new one without failing. The first certificate you generate will likely have had a serial number of 0, so the first time you replace it, set it to 1, then increment each time you replace it after that.
If you want a specific virtualhost to use a certificate open that host configuration file, usually <code>/etc/httpd/conf.d/hostname.conf</code>, and paste this lines between <VirtualHost hostname:port> and </VirtualHost>
 
<pre>SSLEngine on
# cd /etc/pki/tls/certs
SSLCertificateFile /etc/pki/tls/certs/hostname.crt
# make testcert SERIAL=1
SSLCertificateKeyFile /etc/pki/tls/private/hostname.key</pre>
 
Answer the questions as they come up, but don't sweat the answers too much (more detailed documentation can explain what the various fields in a TLS certificate mean, if you are interested). After completing this, you should have new files {{filename|/etc/pki/tls/certs/localhost.crt}} and {{filename|/etc/pki/tls/private/localhost.key}} - the former is your self-signed server certificate, and the latter is the corresponding private key.
 
The default configuration of mod_ssl uses the filenames generated by {{command|make testcert}}, so no further configuration is needed.
 
If you want to have a certificate signed by some authority which will be trusted by others, you can generate a certificate signing request (CSR) instead of a certificate by running {{command|make certreq}} instead of {{command|make testcert}}. You can then provide that CSR to the signing authority and they will provide you with a signed certificate: you should either save this certificate as {{filename|/etc/pki/tls/certs/localhost.crt}}, or save it as another filename and modify the mod_ssl configuration to point to the correct file (see below).
 
=== Install an existing certificate ===
 
If you already have a certificate generated on another computer, move the certificate and the key file to the correct folder, and ensure their SELinux contexts, ownerships and permissions are correct:
 
# mv key_file.key /etc/pki/tls/private/localhost.key
# restorecon /etc/pki/tls/private/localhost.key
# chown root.root /etc/pki/tls/private/localhost.key
# chmod 0600 /etc/pki/tls/private/localhost.key
# mv certificate.crt /etc/pki/tls/certs/localhost.crt
# restorecon /etc/pki/tls/private/localhost.crt
# chown root.root /etc/pki/tls/private/localhost.crt
# chmod 0600 /etc/pki/tls/private/localhost.crt
 
=== Installing webapps ===
 
You probably want to run something on your web server. Many of the most popular 'web applications' are packaged for Fedora. Using the packaged versions of web applications is usually recommended: they will be configured following the distribution's best practices which help to ensure the security of the installation, for instance by installing static files to locations the web server does not have the ability to write to, and doing access control with configuration files rather than {{filename|.htaccess}} files, which are slightly more vulnerable to attack.
 
Packaged web applications will also be configured to work with [[SELinux]], which provides significant security benefits.
 
You will also receive updates through the usual Fedora update process, making it easier to keep your installation up to date.
 
They will also often have the default configuration tweaked according to Fedora's conventions, meaning you have to do less work to get the application up and running.
 
Most web applications are simply packaged according to their name. For example, you can install Wordpress with:
 
# yum install wordpress
 
Packaged web applications will usually provide Fedora-specific instructions in a documentation file - for instance, Wordpress provides the files {{filename|/usr/share/doc/wordpress/README.fedora}} and {{filename|/usr/share/doc/wordpress/README.fedora-multiuser}}. It is always a good idea to read these files!
 
Packaged web applications usually restrict access by default so you can access them only from the server host itself, to ensure you can run all initial configuration safely and things like administration interfaces are not left accessible to the public. For information on how to broaden access, see [[#webapp-access-control|below]].
 
Web applications commonly require the use of a database server. This wiki contains information on installing and configuring [[PostgreSQL]] and [[MariaDB]] on Fedora.


== Configuration ==
== Configuration ==


Configuration files are stored under <code>/etc/httpd/conf.d/</code> and <code>/etc/httpd/conf/httpd.conf</code> is the main configuration file
{{filename|/etc/httpd/conf/httpd.conf}} is the main Apache configuration file. It ''includes'' all the files in {{filename|/etc/httpd/conf.d/}}: if the same setting is specified in both {{filename|/etc/httpd/conf/httpd.conf}} and a file in {{filename|/etc/httpd/conf.d/}}, the setting from the {{filename|/etc/httpd/conf.d/}} file will win. Files in {{filename|/etc/httpd/conf.d/}} are read in alphabetical order: a setting from {{filename|/etc/httpd/conf.d/z-foo.conf}} will win over a setting from {{filename|/etc/httpd/conf.d/foo.conf}}, which will win over a setting from {{filename|/etc/httpd/conf.d/99-foo.conf}}, which will win over a setting from {{filename|/etc/httpd/conf.d/00-foo.conf}}.
 
It is usually best practice never to modify {{filename|/etc/httpd/conf/httpd.conf}} or any of the {{filename|/etc/httpd/conf.d}} files shipped by Fedora packages directly. If you make any local changes to these files, then any changes to them in newer package versions will not be directly applied: instead a {{filename|.rpmnew}} file will be created and you will have to merge the changes manually. It is usually better instead to create a new file in {{filename|/etc/httpd/conf.d}} which will take precedence over the file you wish to 'modify', and make your settings there. For instance, to change a setting specified in {{filename|/etc/httpd/conf.d/foo.conf}} you could create the file {{filename|/etc/httpd/conf.d/z-foo-local.conf}} and place your setting in that file. We will see an example of this next.


=== Opening ports ===
=== TLS/SSL configuration ===


{{admon/warning | This exposes your computer to the Internet and potential attackers. Secure your installation properly before exposing your server to the Internet.}}
The default TLS/SSL configuration is contained in the file {{filename|/etc/httpd/conf.d/ssl.conf}} (if you are using {{package|mod_ssl}}). If you examine that file, you will see the directives that specify where the TLS/SSL certificate and key are located:


Apache uses port 80 for plain http connections and port 443 for SSL connections by default. To make this service available from other computers or the Internet your have to allow Apache through the firewall like this
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key


For plain http connections
If you look carefully, you will see that these directives are actually enclosed in a block defining a [https://httpd.apache.org/docs/current/vhosts/ virtual host]:
<pre>$ firewall-cmd --permanent --add-service=http</pre>


For SSL connections
<VirtualHost _default_:443>
<pre>$ firewall-cmd --permanent --add-service=https</pre>
...
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
...
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
...
</VirtualHost>
 
If we wanted to define a different location for these files, we could edit the lines in {{filename|/etc/httpd/conf.d/ssl.conf}} directly, but it would be better to create a new file {{filename|/etc/httpd/conf.d/z-ssl-local.conf}}:
 
<VirtualHost _default_:443>
SSLCertificateFile /etc/pki/tls/certs/www.myhost.org.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.myhost.org.key
</VirtualHost>
 
This file will override those two settings for the _default_:443 virtual host; all other settings from {{filename|ssl.conf}} will be kept.
 
==== Settings for individual virtual hosts ====
 
If you want a specific virtual host to use SSL/TLS with a different certificate from the default, open that virtual host's configuration file, usually {{filename|/etc/httpd/conf.d/hostname.conf}}, and insert these lines between {{code|<VirtualHost hostname:port>}} and {{code|</VirtualHost>}}:
 
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/hostname.crt
SSLCertificateKeyFile /etc/pki/tls/private/hostname.key</pre>
 
{{anchor|webapp-access-control}}
=== Enabling access to web applications ===
 
Fedora-packaged web applications are usually configured such that, by default, access is allowed only from localhost. Typically you will find that there is a file {{filename|/etc/httpd/conf.d/webapp.conf}} with the following (among other settings):
 
<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require local
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
    </IfModule>
</Directory>
 
Before allowing general access to the webapp, ensure you have configured it correctly and the administration interface and other sensitive areas are not accessible without appropriate authentication. Also remember to ensure your database configuration is secure, if the application uses a database. To broaden access to the application, you can create a file {{filename|/etc/httpd/conf.d/z-webapp-allow.conf}}. To allow access to all systems on a typical local network, you could write:
 
<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require local
        Require ip 192.168.1
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
        Allow from 192.168.1
    </IfModule>
</Directory>
 
Once you are sure the application is correctly configured, this configuration will allow access from any host:
 
<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Allow from all
    </IfModule>
</Directory>
 
{{anchor|firewall-configuration}}
=== Opening firewall ports ===
 
{{admon/warning | This exposes your computer to the Internet and potential attackers. Secure your system and your Apache installation properly before exposing your server to the Internet.}}
 
Apache uses port 80 for plain http connections and port 443 for TLS/SSL connections by default. To make this service available from other computers or the Internet your have to allow Apache through the firewall like this:
 
For plain HTTP connections:
# firewall-cmd --permanent --add-service=http
 
For TLS/SSL connections:
# firewall-cmd --permanent --add-service=https


=== Disable test page ===
=== Disable test page ===


To disable the test page comment out all the lines in this file <code>/etc/httpd/conf.d/welcome.conf</code>
To disable the test page comment out all the lines in the file {{filename|/etc/httpd/conf.d/welcome.conf}}
 
== References ==


== Links ==
* [https://httpd.apache.org/docs/current/ Apache documentation]
* [https://docs.fedoraproject.org/en-US/Fedora/14/html/Deployment_Guide/s1-apache-mod_ssl.html Fedora Documentation, Setting Up an SSL Server]
* [https://httpd.apache.org/docs/current/getting-started.html Apache "Getting Started"]
* [https://httpd.apache.org/docs/current/ssl/ Apache TLS/SSL documentation]
* [https://httpd.apache.org/docs/current/misc/security_tips.html Apache security tips]
* [[OwnCloud]]

Revision as of 02:11, 29 August 2014

The Apache HTTP Server is one of the most commonly-used web servers. This page acts as a quick start guide to deploying and configuring Apache on Fedora. For (many) more details, please see upstream's extensive documentation.

Installation

$ su
# yum install httpd

If you want TLS/SSL support, you can also install Package-x-generic-16.pngmod_ssl, which is based on OpenSSL. Alternatives are Package-x-generic-16.pngmod_gnutls (uses GnuTLS) and Package-x-generic-16.pngmod_nss (uses NSS).

# yum install mod_ssl

To have the server start at each boot:

# systemctl enable httpd

At this point, you should be able to browse to http://localhost on the server and access the Apache test page. You will most likely not be able to access the server from any other host, yet: we will change this later.

Create a self signed TLS/SSL certificate

If you want to use TLS/SSL, you will need a server certificate. The simplest choice is to create a 'self-signed' certificate; this does not require you to deal with a certificate signing authority, but means no system will trust your server by default. You may publish and/or note down the key's fingerprint and verify it each time you connect to your server.

# cd /etc/pki/tls/certs
# make testcert

If your server already has a valid certificate and you want to replace it, pass SERIAL= to set a different serial number so that clients notice the change in certificate and update to the new one without failing. The first certificate you generate will likely have had a serial number of 0, so the first time you replace it, set it to 1, then increment each time you replace it after that.

# cd /etc/pki/tls/certs
# make testcert SERIAL=1

Answer the questions as they come up, but don't sweat the answers too much (more detailed documentation can explain what the various fields in a TLS certificate mean, if you are interested). After completing this, you should have new files /etc/pki/tls/certs/localhost.crt and /etc/pki/tls/private/localhost.key - the former is your self-signed server certificate, and the latter is the corresponding private key.

The default configuration of mod_ssl uses the filenames generated by make testcert, so no further configuration is needed.

If you want to have a certificate signed by some authority which will be trusted by others, you can generate a certificate signing request (CSR) instead of a certificate by running make certreq instead of make testcert. You can then provide that CSR to the signing authority and they will provide you with a signed certificate: you should either save this certificate as /etc/pki/tls/certs/localhost.crt, or save it as another filename and modify the mod_ssl configuration to point to the correct file (see below).

Install an existing certificate

If you already have a certificate generated on another computer, move the certificate and the key file to the correct folder, and ensure their SELinux contexts, ownerships and permissions are correct:

# mv key_file.key /etc/pki/tls/private/localhost.key
# restorecon /etc/pki/tls/private/localhost.key
# chown root.root /etc/pki/tls/private/localhost.key
# chmod 0600 /etc/pki/tls/private/localhost.key
# mv certificate.crt /etc/pki/tls/certs/localhost.crt
# restorecon /etc/pki/tls/private/localhost.crt
# chown root.root /etc/pki/tls/private/localhost.crt
# chmod 0600 /etc/pki/tls/private/localhost.crt

Installing webapps

You probably want to run something on your web server. Many of the most popular 'web applications' are packaged for Fedora. Using the packaged versions of web applications is usually recommended: they will be configured following the distribution's best practices which help to ensure the security of the installation, for instance by installing static files to locations the web server does not have the ability to write to, and doing access control with configuration files rather than .htaccess files, which are slightly more vulnerable to attack.

Packaged web applications will also be configured to work with SELinux, which provides significant security benefits.

You will also receive updates through the usual Fedora update process, making it easier to keep your installation up to date.

They will also often have the default configuration tweaked according to Fedora's conventions, meaning you have to do less work to get the application up and running.

Most web applications are simply packaged according to their name. For example, you can install Wordpress with:

# yum install wordpress

Packaged web applications will usually provide Fedora-specific instructions in a documentation file - for instance, Wordpress provides the files /usr/share/doc/wordpress/README.fedora and /usr/share/doc/wordpress/README.fedora-multiuser. It is always a good idea to read these files!

Packaged web applications usually restrict access by default so you can access them only from the server host itself, to ensure you can run all initial configuration safely and things like administration interfaces are not left accessible to the public. For information on how to broaden access, see below.

Web applications commonly require the use of a database server. This wiki contains information on installing and configuring PostgreSQL and MariaDB on Fedora.

Configuration

/etc/httpd/conf/httpd.conf is the main Apache configuration file. It includes all the files in /etc/httpd/conf.d/: if the same setting is specified in both /etc/httpd/conf/httpd.conf and a file in /etc/httpd/conf.d/, the setting from the /etc/httpd/conf.d/ file will win. Files in /etc/httpd/conf.d/ are read in alphabetical order: a setting from /etc/httpd/conf.d/z-foo.conf will win over a setting from /etc/httpd/conf.d/foo.conf, which will win over a setting from /etc/httpd/conf.d/99-foo.conf, which will win over a setting from /etc/httpd/conf.d/00-foo.conf.

It is usually best practice never to modify /etc/httpd/conf/httpd.conf or any of the /etc/httpd/conf.d files shipped by Fedora packages directly. If you make any local changes to these files, then any changes to them in newer package versions will not be directly applied: instead a .rpmnew file will be created and you will have to merge the changes manually. It is usually better instead to create a new file in /etc/httpd/conf.d which will take precedence over the file you wish to 'modify', and make your settings there. For instance, to change a setting specified in /etc/httpd/conf.d/foo.conf you could create the file /etc/httpd/conf.d/z-foo-local.conf and place your setting in that file. We will see an example of this next.

TLS/SSL configuration

The default TLS/SSL configuration is contained in the file /etc/httpd/conf.d/ssl.conf (if you are using Package-x-generic-16.pngmod_ssl). If you examine that file, you will see the directives that specify where the TLS/SSL certificate and key are located:

SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

If you look carefully, you will see that these directives are actually enclosed in a block defining a virtual host:

<VirtualHost _default_:443>
...
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
...
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
...
</VirtualHost>

If we wanted to define a different location for these files, we could edit the lines in /etc/httpd/conf.d/ssl.conf directly, but it would be better to create a new file /etc/httpd/conf.d/z-ssl-local.conf:

<VirtualHost _default_:443>
SSLCertificateFile /etc/pki/tls/certs/www.myhost.org.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.myhost.org.key
</VirtualHost>

This file will override those two settings for the _default_:443 virtual host; all other settings from ssl.conf will be kept.

Settings for individual virtual hosts

If you want a specific virtual host to use SSL/TLS with a different certificate from the default, open that virtual host's configuration file, usually /etc/httpd/conf.d/hostname.conf, and insert these lines between <VirtualHost hostname:port> and </VirtualHost>:

SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/hostname.crt

SSLCertificateKeyFile /etc/pki/tls/private/hostname.key

Enabling access to web applications

Fedora-packaged web applications are usually configured such that, by default, access is allowed only from localhost. Typically you will find that there is a file /etc/httpd/conf.d/webapp.conf with the following (among other settings):

<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require local
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
    </IfModule>
</Directory>

Before allowing general access to the webapp, ensure you have configured it correctly and the administration interface and other sensitive areas are not accessible without appropriate authentication. Also remember to ensure your database configuration is secure, if the application uses a database. To broaden access to the application, you can create a file /etc/httpd/conf.d/z-webapp-allow.conf. To allow access to all systems on a typical local network, you could write:

<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require local
        Require ip 192.168.1
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
        Allow from 192.168.1
    </IfModule>
</Directory>

Once you are sure the application is correctly configured, this configuration will allow access from any host:

<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Allow from all
    </IfModule>
</Directory>

Opening firewall ports

Warning.png
This exposes your computer to the Internet and potential attackers. Secure your system and your Apache installation properly before exposing your server to the Internet.

Apache uses port 80 for plain http connections and port 443 for TLS/SSL connections by default. To make this service available from other computers or the Internet your have to allow Apache through the firewall like this:

For plain HTTP connections:

# firewall-cmd --permanent --add-service=http

For TLS/SSL connections:

# firewall-cmd --permanent --add-service=https

Disable test page

To disable the test page comment out all the lines in the file /etc/httpd/conf.d/welcome.conf

References