Apache

From LinuxReviews
Jump to navigationJump to search
Apache HTTP Server
Apache-icon.png
Original author(s)Robert McCool
Developer(s)Apache Software Foundation
Initial release1995; 26 years ago (1995)
RepositoryApache HTTP Server Repository
Written inC, XML
Operating systemUnix-like, Microsoft Windows
TypeWeb server
LicenseApache License 2.0
Internet-web-browser.svg

Apache ("Apache HTTP Server", or httpd) is a tried and true modular free open source web server with powerful features. It is mostly used to serve PHP scripts, static pages and images. Many widely used content management systems like WordPress and MediaWiki can be served using the Apache web server. Apache powered about half the websites on the Internet Q3 2020.

Apache does not have the ability to run server-side JavaScript the way node.js does.

Apache is a modular web server and there are a lot of modules available. It can have a very small memory footprint if a minimal amount of modules are loaded and it can have a lot of features and a higher memory footprint if a huge variety of modules are loaded.

Tips

You can get free HTTPS certificates for Apache from Let's Encrypt. Using https with a valid certificate is almost a requirement for hosting any even remotely popular website.

Apache is best restarted by running:

apachectl configtest
apachectl graceful

You can restart Apache by running apachectl restart or systemd by running service httpd restart - but that will immediately close all the webservers connections and potentially cause failed page loads for those visiting sites running on the webservers. apachectl graceful will keep current connections open. Restarting this way takes longer but it is also safer.

Configuration

Modern Apache version support three different worker modules:

  • prefork MPM (doc)
  • worker MPM (doc)
  • event MPM (doc)

The event MPM works really well with both php-fpm and php-zts.

PHP

Apache 2.4 supports opcache. It should be enabled in /etc/httpd/conf.d/php.conf with:

    php_value opcache.file_cache   "/var/lib/php/opcache"

Make sure the user apache is running as can read and write to the directory you choose. The performance difference between a opcahce (compiled PHP cache) and no cache really is quite huge.

Modern PHP modules for Apache support APCu memory cache. You should absolutely enable this if the web application you use supports because it really is very efficient.

Mod Rewrite Tricks

You can turn *.php? into */ with:

RewriteRule ^([^/\.]+)/?$ $1.php [L]

Checking Apache's Status

You can check if systemd believes Apache is running on Debian and Ubuntu with:

sudo systemctl status apache2

The equivalent on AlmaLinux, Fedora, CentOS and RHEL is as to, as root, run:

systemctl status httpd

Apache comes with a utility called apachectl. Running apachectl status will probably just redirect to systemctl status httpd on modern distributions (it used to show output from apachectl).

Another, a bit more complicated way, to check the status of a Apache server is to make it show a status page for you. This can be done by means of the mod_status module. You would need this to a Apache configuration file (a main one, not a per-vost one) to load it:

LoadModule status_module modules/mod_status.so

That is not enough. You will also need a configuration file for it. That file can be /etc/apache2/mods-enabled/status.conf (Debian/Ubuntu) or /etc/httpd/conf.d/server-status.conf (AlmaLinux, RHEL, Fedora). The contents of it should be something like:

File: /etc/httpd/conf.d/server-status.conf
<IfModule mod_status.c>
  <VirtualHost 127.0.0.1:80>
  <Location /server-status>
    SetHandler server-status
    Require local #restricts access to localhost
  </Location>
  </VirtualHost>
</Ifmodule>

This assumes you do not have any other vhost on 127.0.0.1.

You could, instead, just add:

File: /etc/httpd/conf.d/existing-vhost.conf
<IfModule mod_status.c>
  <Location /server-status>
    SetHandler server-status
    Require local #restricts access to localhost
  </Location>
</Ifmodule>

..to any existing Apache configuration file.

You should check if your new configuration is valid with apachectl -t before you make Apache load it with apachectl graceful (or apachectl restart if you don't care about concurrent users).

Common Problems

"(28)No space left on device: mod_rewrite: could not create rewrite_log_lock": This error can occur if Apache shuts down in a dirty (not normal) fashion. The cause of this error is semaphore-arrays left over after an unclean shutdown. You can check what, if anything, is left behind with ipcs -s | grep apache and remove the semaphore-arrays by running:

ipcs -s | grep apache | perl -e 'while (<STDIN>) { @a=split(/\s+/); print `ipcrm sem $a[1]`}'

Links

Apache has a homepage at httpd.apache.org.


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.