Apache: Difference between revisions

From MoodleDocs
(mod_rewrite has to be enabled to make this work)
 
(63 intermediate revisions by 28 users not shown)
Line 1: Line 1:
When most people refer to '''Apache''' they are talking about the "Apache HTTP Server Project". In fact Apache is really the "Apache Software Foundation" which hosts a long list of projects of which the HTTP server is just one, albeit the best known.
{{Installing Moodle}}
{{Update}}
'''This article refers to the 'Apache HTTP server''''


The Apache HTTP Server enables web pages to be published on the internet (or an intranet or even on a single test machine). It is tightly linked with the [[PHP]] scripting language in which Moodle is developed. A web server is a required component of a Moodle installation. There are two distinct developments of the Apache HTTP Server, version 1 and version 2. Although version 2 should have replaced version 1, the architectures are so different that they both continue to be supported. Either are fully supported by Moodle.
The Apache HTTP server is the software that (along with the PHP scripting language) 'runs' Moodle. Note that there are alternatives (e.g. IIS on Windows, Nginx on Linux, MacOS) but the Apache HTTP Server is very popular on all platforms.
== Installing Apache ==
Installers are available for most platforms from http://httpd.apache.org/download.cgi. The official installation instructions are here: http://httpd.apache.org/docs/2.0/install.html. If you are running Linux then you are recommended to use the packaged version if you can. For example, in Debian/Ubuntu it is simply:
<pre>
sudo apt-get install apache2
</pre>
See the documentation for your particular platform for the instructions. Apache is straightforward to build from source if you have to and the PHP documentation contains an article on building both Apache and PHP together - although you should rarely need to do that.
==Performance==
See [[Performance recommendations]]
==Slasharguments==
The function ''slash arguments'' is required for various features in Moodle to work correctly, as described in [[Using slash arguments]].


The Apache HTTP Server Project describes itself thus:
To turn it on, add this line to your ''httpd.conf'', or to a ''.htaccess'' file in your local directory:
<syntaxhighlight lang="apacheconf">
AcceptPathInfo On
</syntaxhighlight>
''Note:'' When using ".htaccess" in your local Moodle install folder, you may need to include/enable "AllowOverride Directive" in "httpd.conf", first.


"The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems including UNIX and Windows NT. The goal of this project is to provide a secure, efficient and extensible server that provides HTTP services in sync with the current HTTP standards.
''Note:'' Using .htaccess file will cause performance hit on your server!


Apache has been the most popular web server on the Internet since April 1996. The November 2005 Netcraft Web Server Survey found that more than 70% of the web sites on the Internet are using Apache, thus making it more widely used than all other web servers combined."
If you are using Ionos (formerly 1&1) shared webhosting, the above does not work, there is a known bug when using PHP as CGI. The solution is to create a php.ini file in the moodle directory with this content:
<syntaxhighlight lang="ini">
cgi.fix_pathinfo = 0
</syntaxhighlight>
Also Ionos requires that this php.ini be in every directory that a script executes. Use the procedure below to link a php.ini in every subdirectory back to your original php.ini file.
<syntaxhighlight lang="bash">
cd your_moodle_directory
find -type d -exec ln -s $PWD/php.ini {}/php.ini \;
</syntaxhighlight>
Source: [https://www.ionos.com/help/hosting/using-php-for-web-projects/applying-php-settings-to-all-subdirectories/ Ionos php.ini Help]


==Configuration==
This may affect other shared hosting providers as well.
== Handling 40x errors  ==
This enables missing files to be themed by Moodle
<pre>
ErrorDocument 404 /error/index.php
# This sends any 403 from apache through to the same page, but also
# overrides the http status with 404 instead for better security.
ErrorDocument 403 /error/index.php?code=404
</pre>
== Hiding internal paths ==
Enabling mod_rewrite (required on Ubuntu) , then restart Apache<syntaxhighlight lang="shell">
sudo a2enmod rewrite


If you have the luxury of being able to configure Apache directly, you can achieve a major performance boost by specifying that images, JavaScript and other often-reused elements must be cached locally. This will enormously reduce both page loading time and server load.
sudo systemctl restart apache2
</syntaxhighlight><pre>
RewriteEngine On
RewriteRule "(\/vendor\/)" - [F]
RewriteRule "(\/node_modules\/)" - [F]
RewriteRule "(^|/)\.(?!well-known\/)" - [F]
RewriteRule "(composer\.json)" - [F]
RewriteRule "(\.lock)" - [F]
RewriteRule "(\/environment.xml)" - [F]
Options -Indexes
RewriteRule "(\/install.xml)" - [F]
RewriteRule "(\/README)" - [F]
RewriteRule "(\/readme)" - [F]
RewriteRule "(\/moodle_readme)" - [F]
RewriteRule "(\/upgrade\.txt)" - [F]
RewriteRule "(phpunit\.xml\.dist)" - [F]
RewriteRule "(\/tests\/behat\/)" - [F]
RewriteRule "(\/fixtures\/)" - [F]
</pre>
''Note:'' Please pay attention that above rules may block loading desired/required content, e.g. SCORM-packages containing a "vendor"-directory.
==SSL==
Moodle has an option to enable HTTPS for the whole site or for just the login pages; either option requires that your web server is configured for SSL.
* Whole site HTTPS is enabled by changing http://<url> to https:// <url> in your config.php 'wwwroot' parameter.
* Login only HTTPS is enabled by setting the 'loginhttps' parameter, where the wwwroot schema should remain as http://
NOTE: Login only https was deprecated and removed from Moodle 3.4: https://tracker.moodle.org/browse/MDL-42834


===Apache 2.x - Using mod_expires to improve performance===
Login only https is available in Moodle 3.3 and earlier in the admin interface via Administration>Security>HTTP Security and checking the button. (Note the warning and see ssl section below)


(Note that these instructions may only apply to Debian Linux. Users of other systems should check their documentation to check that mod_expires is loaded)
Prior to Moodle 2.3 It was not advised to run the whole site over HTTPS due to legacy restrictions with client-side caching. This is no longer the case assuming client browsers support the 'Cache-Control: public' method, which all supported browsers for this version of Moodle do.


from the command line:
To use HTTPS you will need to obtain an SSL certificate, you have two options:
 
* Generate a self-signed certificate. This is fine on (say) an Intranet but unsuitable for the public internet, but users will we warned the certificated is untrusted when used publicly.
a2enmod expires
* Purchase a certificate from a vendor. There is a surprising range of prices and value-added services available. Some hosting companies even provide free certificates.  
 
Debian provides instructions for installing a self-signed certificate [https://wiki.debian.org/Self-Signed_Certificate on their wiki] and includes general information on configuring Apache for SSL.
you can now add the following lines inside the <virtualhost> tags of the relevant file in apache2/sites-enabled
If you purchase a vendor certificate you will normally receive instructions for installing it.
 
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType text/html "access plus 1 seconds"
  ExpiresByType image/gif "access plus 120 minutes"
  ExpiresByType image/jpeg "access plus 120 minutes"
  ExpiresByType image/png "access plus 120 minutes"
  ExpiresByType text/css "access plus 60 minutes"
  ExpiresByType text/javascript "access plus 60 minutes"
  ExpiresByType application/x-javascript "access plus 60 minutes"
  ExpiresByType text/xml "access plus 60 minutes"
</IfModule>
 
then restart apache with something like
 
/etc/init.d/apache2 restart
 
The downside is that if you're editing your theme, there may be confusion as you'll keep seeing the old images instead of the new ones. Clicking 'refresh' in the browser or clearing the cache should sort this out.
 
[http://www.metaskills.net/blog/heuristics/sysadmin/how-to-control-browser-caching-with-apache-2 more information]


A basic Apache SSL configuration can be summarised as:
Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
    SSLEngine On
    SSLCertificateFile /path/to/your/certificate.crt
    SSLCertificateKeyFile /path/to/your/certificate.key
    ...
</VirtualHost>
== Load Balancer Hints (AWS) ==
If you're using an AWS load balancer in front your infrastructure, you can set up some of the configuration above, preventing traffic to go forward. Here is the configuration applied to hide files, with a few considerations due to known limitations:
* 100 total rules per application load balancer
* 5 condition values per rule
* 5 wildcards per rule
* 5 weighted target groups per rule:
<small>
<pre>
[
    {
        "Conditions": [
            {
                "Field": "path-pattern",
                "Values": [
                    "*/.*",
                    "*/upgrade.txt",
                    "*/db/install.xml",
                    "*/README.md"
                ],
                "PathPatternConfig": {
                    "Values": [
                        "*/.*",
                        "*/upgrade.txt",
                        "*/db/install.xml",
                        "*/README.md"
                    ]
                }
            }
        ],
        "Actions": [
            {
                "Type": "fixed-response",
                "Order": 1,
                "FixedResponseConfig": {
                    "ContentType": "text/html",
                    "MessageBody": "<html>\n<head><title>404 Not Found</title></head>\n<body>\n<center><h1>404 Not Found</h1></center>\n<hr>\n</body>\n</html>",
                    "StatusCode": "404"
                }
            }
        ]
    },
    {
        "Conditions": [
            {
                "Field": "path-pattern",
                "Values": [
                    "*/composer.json",
                    "*/Gruntfile.js",
                    "*.lock",
                    "*/environtment.xml",
                    "*/readme.txt"
                ],
                "PathPatternConfig": {
                    "Values": [
                        "*/composer.json",
                        "*/Gruntfile.js",
                        "*.lock",
                        "*/environtment.xml",
                        "*/readme.txt"
                    ]
                }
            }
        ],
        "Actions": [ #### Same as above
            ...
        ]
    },
    {
        "Conditions": [
            {
                "Field": "path-pattern",
                "Values": [
                    "*/fixtures/*",
                    "*/behat/*",
                    "*/phpunit.xml"
                ],
                "PathPatternConfig": {
                    "Values": [
                        "*/fixtures/*",
                        "*/behat/*",
                        "*/phpunit.xml"
                    ]
                }
            }
        ],
        "Actions": [ #### Same as above
            ...
        ]
    }
]
</pre></small>
== See also ==
== See also ==
* [http://httpd.apache.org/ The Apache HTTP Server Project homepage]
* [http://httpd.apache.org/ The Apache HTTP Server Project homepage]
* [http://en.wikipedia.org/wiki/Apache_HTTP_Server Wikipedia article on the Apache HTTP Server]
* [http://en.wikipedia.org/wiki/Apache_HTTP_Server Wikipedia article on the Apache HTTP Server]
 
* [http://httpd.apache.org/docs/2.0/misc/perf-tuning.html Apache Performance Tuning article at the official homepage]
 
* [https://els.earlham.edu/cayaraa/weblog/1468.html Making Moodle work with SSL]
[[Category:Administrator]]
* [http://www.krufix.de/ Using the same Moodle twice in local network and Internet via SSL-Proxy] (in German)
[[Category:Developer]]
[[pl:Apache]]
 
[[ja:Apache]]
{{stub}}
[[de:Apache]]
[[es:Apache]]

Latest revision as of 23:37, 21 December 2022

This page requires updating. Please do so and remove this template when finished.


This article refers to the 'Apache HTTP server'

The Apache HTTP server is the software that (along with the PHP scripting language) 'runs' Moodle. Note that there are alternatives (e.g. IIS on Windows, Nginx on Linux, MacOS) but the Apache HTTP Server is very popular on all platforms.

Installing Apache

Installers are available for most platforms from http://httpd.apache.org/download.cgi. The official installation instructions are here: http://httpd.apache.org/docs/2.0/install.html. If you are running Linux then you are recommended to use the packaged version if you can. For example, in Debian/Ubuntu it is simply:

sudo apt-get install apache2

See the documentation for your particular platform for the instructions. Apache is straightforward to build from source if you have to and the PHP documentation contains an article on building both Apache and PHP together - although you should rarely need to do that.

Performance

See Performance recommendations

Slasharguments

The function slash arguments is required for various features in Moodle to work correctly, as described in Using slash arguments.

To turn it on, add this line to your httpd.conf, or to a .htaccess file in your local directory:

AcceptPathInfo On

Note: When using ".htaccess" in your local Moodle install folder, you may need to include/enable "AllowOverride Directive" in "httpd.conf", first.

Note: Using .htaccess file will cause performance hit on your server!

If you are using Ionos (formerly 1&1) shared webhosting, the above does not work, there is a known bug when using PHP as CGI. The solution is to create a php.ini file in the moodle directory with this content:

cgi.fix_pathinfo = 0

Also Ionos requires that this php.ini be in every directory that a script executes. Use the procedure below to link a php.ini in every subdirectory back to your original php.ini file.

cd your_moodle_directory
find -type d -exec ln -s $PWD/php.ini {}/php.ini \;

Source: Ionos php.ini Help

This may affect other shared hosting providers as well.

Handling 40x errors

This enables missing files to be themed by Moodle

ErrorDocument 404 /error/index.php
 
# This sends any 403 from apache through to the same page, but also
# overrides the http status with 404 instead for better security.
ErrorDocument 403 /error/index.php?code=404

Hiding internal paths

Enabling mod_rewrite (required on Ubuntu) , then restart Apache

sudo a2enmod rewrite

sudo systemctl restart apache2
 
RewriteEngine On
 
RewriteRule "(\/vendor\/)" - [F]
RewriteRule "(\/node_modules\/)" - [F]
RewriteRule "(^|/)\.(?!well-known\/)" - [F]
RewriteRule "(composer\.json)" - [F]
RewriteRule "(\.lock)" - [F]
RewriteRule "(\/environment.xml)" - [F]
Options -Indexes
RewriteRule "(\/install.xml)" - [F]
RewriteRule "(\/README)" - [F]
RewriteRule "(\/readme)" - [F]
RewriteRule "(\/moodle_readme)" - [F]
RewriteRule "(\/upgrade\.txt)" - [F]
RewriteRule "(phpunit\.xml\.dist)" - [F]
RewriteRule "(\/tests\/behat\/)" - [F]
RewriteRule "(\/fixtures\/)" - [F]

Note: Please pay attention that above rules may block loading desired/required content, e.g. SCORM-packages containing a "vendor"-directory.

SSL

Moodle has an option to enable HTTPS for the whole site or for just the login pages; either option requires that your web server is configured for SSL.

  • Whole site HTTPS is enabled by changing http://<url> to https:// <url> in your config.php 'wwwroot' parameter.
  • Login only HTTPS is enabled by setting the 'loginhttps' parameter, where the wwwroot schema should remain as http://

NOTE: Login only https was deprecated and removed from Moodle 3.4: https://tracker.moodle.org/browse/MDL-42834

Login only https is available in Moodle 3.3 and earlier in the admin interface via Administration>Security>HTTP Security and checking the button. (Note the warning and see ssl section below)

Prior to Moodle 2.3 It was not advised to run the whole site over HTTPS due to legacy restrictions with client-side caching. This is no longer the case assuming client browsers support the 'Cache-Control: public' method, which all supported browsers for this version of Moodle do.

To use HTTPS you will need to obtain an SSL certificate, you have two options:

  • Generate a self-signed certificate. This is fine on (say) an Intranet but unsuitable for the public internet, but users will we warned the certificated is untrusted when used publicly.
  • Purchase a certificate from a vendor. There is a surprising range of prices and value-added services available. Some hosting companies even provide free certificates.

Debian provides instructions for installing a self-signed certificate on their wiki and includes general information on configuring Apache for SSL. If you purchase a vendor certificate you will normally receive instructions for installing it.

A basic Apache SSL configuration can be summarised as:

Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
    SSLEngine On
    SSLCertificateFile /path/to/your/certificate.crt
    SSLCertificateKeyFile /path/to/your/certificate.key
    ...
</VirtualHost>

Load Balancer Hints (AWS)

If you're using an AWS load balancer in front your infrastructure, you can set up some of the configuration above, preventing traffic to go forward. Here is the configuration applied to hide files, with a few considerations due to known limitations:

  • 100 total rules per application load balancer
  • 5 condition values per rule
  • 5 wildcards per rule
  • 5 weighted target groups per rule:

[
    {
        "Conditions": [
            {
                "Field": "path-pattern", 
                "Values": [
                    "*/.*", 
                    "*/upgrade.txt", 
                    "*/db/install.xml",
                    "*/README.md"
                ], 
                "PathPatternConfig": {
                    "Values": [
                        "*/.*", 
                        "*/upgrade.txt", 
                        "*/db/install.xml",
                        "*/README.md"
                    ]
                }
            }
        ], 
        "Actions": [
            {
                "Type": "fixed-response", 
                "Order": 1, 
                "FixedResponseConfig": {
                    "ContentType": "text/html", 
                    "MessageBody": "<html>\n<head><title>404 Not Found</title></head>\n<body>\n<center><h1>404 Not Found</h1></center>\n<hr>\n</body>\n</html>", 
                    "StatusCode": "404"
                }
            }
        ]
    }, 
    {
        "Conditions": [
            {
                "Field": "path-pattern", 
                "Values": [
                    "*/composer.json", 
                    "*/Gruntfile.js",
                    "*.lock", 
                    "*/environtment.xml",
                    "*/readme.txt"
                ], 
                "PathPatternConfig": {
                    "Values": [
                        "*/composer.json", 
                        "*/Gruntfile.js",
                        "*.lock", 
                        "*/environtment.xml",
                        "*/readme.txt"
                    ]
                }
            }
        ], 
        "Actions": [ #### Same as above
            ...
        ]
    }, 
    {
        "Conditions": [
            {
                "Field": "path-pattern", 
                "Values": [
                    "*/fixtures/*", 
                    "*/behat/*", 
                    "*/phpunit.xml"
                ], 
                "PathPatternConfig": {
                    "Values": [
                        "*/fixtures/*", 
                        "*/behat/*", 
                        "*/phpunit.xml"
                    ]
                }
            }
        ], 
        "Actions": [ #### Same as above
            ...
        ]
    }
]

See also