How To Configure the Apache Web Server on an Ubuntu or Debian VPS
Source: DigitalOcean
By Justin Ellingwood and Anish Singh Walia Apache is one of the most popular web servers on the internet. It is used to serve more than half of all active websites. Although there are many viable web servers that will serve your content, it is helpful to understand how Apache works because of its ubiquity. This article will examine some general configuration files and the options that can be controlled within them. This article will follow the Ubuntu/Debian layout of Apache files, which is different from how other distributions build the configuration hierarchy. Version Compatibility: This tutorial has been validated and tested on Ubuntu 22.04 LTS, Ubuntu 24.04 LTS, and Ubuntu 25.04, as well as Debian 11 and Debian 12. All commands and configurations shown in this guide are compatible with these versions. The Apache configuration structure and commands (such as a2ensite, a2dissite, a2enmod, and a2dismod) work consistently across these Ubuntu and Debian versions. Modular Configuration Structure: Apache on Ubuntu/Debian uses a modular configuration system with separate directories for sites (sites-available/ and sites-enabled/), modules (mods-available/ and mods-enabled/), and configuration fragments (conf-available/ and conf-enabled/), making it easy to enable or disable features without editing monolithic files. Virtual Host Management: Virtual hosts allow you to host multiple websites on a single server. The a2ensite and a2dissite commands manage site configurations by creating or removing symbolic links, while the ServerName and ServerAlias directives control which domains each virtual host handles. Global Configuration Options: Key performance settings like Timeout, KeepAlive, MaxKeepAliveRequests, and KeepAliveTimeout in the main apache2.conf file can significantly impact server performance and should be tuned based on your traffic patterns and server resources. Directory Security: Apache applies directory configurations from most specific to least specific, allowing you to set baseline security rules (like denying all access) and then grant access to specific directories. The AllowOverride directive controls whether .htaccess files can override server settings. Module System: Apache’s module system allows you to extend functionality by enabling or disabling modules with a2enmod and a2dismod. The event MPM (Multi-Processing Module) is the default on modern Ubuntu/Debian installations and provides efficient handling of concurrent connections. If you are using Ubuntu version 16.04 or below, we recommend you upgrade to a more recent version since Ubuntu no longer provides support for these versions. This collection of guides will help you in upgrading your Ubuntu version. A server running Ubuntu, along with a non-root user with sudo privileges and an active firewall. For guidance on how to set these up, please choose your distribution from this list and follow our Initial Server Setup Guide. Before you begin exploring your Apache configurations, you should have Apache installed on your server. You can learn how by following our installation tutorials: How To Install the Apache Web Server on Ubuntu, How To Install the Apache Web Server on Debian. Apache keeps its main configuration files within the /etc/apache2 folder. Executing the following command will list all of the files within this folder: There are a number of plaintext files and some subdirectories within this directory. Here are some useful locations to be familiar with: Apache configuration does not take place in a single monolithic file, but instead happens through a modular design where new files can be added and modified as needed. The main configuration details for your Apache server are held in the /etc/apache2/apache2.conf file.
This file is divided into three main sections: Open this file with your preferred text editor. The following example uses nano: In Ubuntu and Debian, this file is used to configure global definitions. The configuration of the default server and virtual hosts are handled by using the Include directive. The Include directive allows Apache to read other configuration files into the current file at the location that the statement appears. The result is that Apache dynamically generates an overarching configuration file on startup. Found within this file are a number of different Include and IncludeOptional statements. These directives load module definitions, the ports.conf document, the specific configuration files in the conf-enabled/ directory, and the virtual host definitions in the sites-enabled/ directory: There are some options you may want to modify in the Global Configuration: By default, this parameter is set to 300. This means that the server has a maximum of 300 seconds to fulfill each request.
This parameter can safely be dropped to something between 30 and 60 seconds. This option, if set to On, will allow each connection to remain open to handle multiple requests from the same client.
If this is set to Off, each request will have to establish a new connection, which can result in significant overhead depending on your setup and traffic situation. This controls how many separate requests each connection will handle before dying. Keeping this number high will allow Apache to serve content to each client more effectively.
The default setting is set to 100. Setting this value to 0 will allow Apache to serve an unlimited amount of requests for each connection. This setting specifies how long to wait for the next request after finishing the last one. If the timeout threshold is reached, then the connection will die.
This means that the next time content is requested, the server will establish a new connection to handle the request for the content that makes up the page the client is visiting. The default is set to 5. After examining the contents of this configuration file, you can close out of it by pressing CTRL+X. A Multi-Processing Module (MPM) extends Apache’s modular design. MPMs are responsible for listening, directing, and handling different network requests. You can cross-reference which section your Apache installation was compiled in with using the following command: You can check the MPM type on your server with the a2query -M command: The output reveals that the event MPM is used on this server. Your installation may have multiple to choose from, but only one can be selected. The default virtual host declaration can be found in a file called 000-default.conf within the sites-available/ directory. You can learn about the general format of a virtual host file by examining this file. Open the file with the following command: The default virtual host is configured to handle any request on port 80, the standard HTTP port. This is defined in the declaration header where it says *:80, meaning port 80 on any interface.
However, this does not mean that it will necessarily handle each request to the server on this port. Apache uses the most specific virtual host definition that matches the request. If there was a more specific definition, it could supersede this definition. After examining the file, you can close out of it by pressing CTRL+X. The following options are set within the virtual host definition outside of any other lower level sub-declaration. They apply to the whole virtual host.
To start, open up the security.conf file within the conf-available/ directory: This file contains the Server Signature directive, which allows you to specify a contact email that should be used when there are server problems. You can change the default option from On to EMail to reveal the server admin email address. Make sure you are willing to receive the mail if you adjust this setting: Exit the file by pressingCTRL+X. After editing a configuration file, a prompt will ask you to confirm your changes. Press Y to save the changes to your file or press N to discard them. Within your virtual host file, you can add a ServerName directive that specifies the domain name or IP address that this request should handle. This is the option that would add specificity to the virtual host, allowing it to override the default definition if it matches the ServerName value. Run the following command to open your virtual host file, making sure to replace the your_domain variable with your actual domain name: Append your_domain to the ServerName directive: Likewise, you can also make the virtual host apply to more than one name by using the ServerAlias directive. This provides alternate paths to get to the same content. A good use case for this is adding the same domain, preceded by www: The DocumentRoot directive specifies where the content that is requested for this virtual host will be located. On Ubuntu, the default virtual host is set up to serve content out of the /var/www/ directory: Within the virtual host definition, there are definitions for how the server handles different directories within the file system. Apache will apply all of these directions in order from shortest to longest, so there is again a chance to override previous options. Open the apache2.conf file with this command: The first directory definition applies rules for the /, or root, directory. This will provide the baseline configuration for your virtual host, as it applies to all files served on the file system. Notice the directory configuration options, along with some helpful comments, contained within this file. This default configuration denies access to all content unless specified otherwise in subsequent directory definitions. The Require directive can restrict or open access to different resources within your server. The AllowOverride directive is used to decide whether an .htaccess file can override settings if it is placed in the content directory. This is not allowed by default, but can be useful to enable in a variety of circumstances. For more information on securing your Apache server, refer to our Initial Server Setup Guide.
After examining the contents of this file, you can close out of it by pressing CTRL+X. Directory definitions are sometimes preceded by Alias or ScriptAlias directives.
Open your virtual host configuration file with this command and replace the your_domain variable with your domain name: The Alias directive maps a URL path to a directory path. For example, in a virtual host that handles requests to your_domain the following would allow access to content within /usr/local/apache/content/ when navigating to your_domain.com/content/: The ScriptAlias directive operates in the same way, but is used to define directories that will have executable components in them: Remember to define the directory with access privileges as discussed in the previous section. After completing your edits on the file, exit the file by pressing CTRL+X. If you made any changes to this file, press Y to save the changes to your file or press N to leave the file as it was before any changes to the configuration. Once you have a virtual host file that meets your requirements, you can use the tools included with Apache to transition it into live websites. To create a symbolic link in the sites-enabled directory to an existing file in the sites-available directory, issue the following command. Make sure to replace your_domain with the name of your own virtual host site configuration file: After enabling a site, issue the following command to tell Apache to reload its configuration files, allowing the change to propagate: There is also a companion command for disabling a virtual host. It operates by removing the symbolic link from the sites-enabled directory. For example, with your virtual host site enabled, you can disable the default 000-default site: Modules can be enabled or disabled by using the a2enmod and a2dismod commands respectively. They work in the same way as the a2ensite and a2dissite versions of these commands. For example, to enable the info module, you can use the following command: Likewise, you can disable a module using the a2dismod command: Remember to restart Apache after modifying configuration files and enabling or disabling modules. Before restarting Apache, you can test your configuration for syntax errors using the apache2ctl command: This command will check your configuration files and report any syntax errors. If the configuration is valid, you’ll see Syntax OK. If there are errors, the output will indicate which file and line number contains the problem. You can set up multiple virtual hosts by creating separate configuration files in the /etc/apache2/sites-available/ directory for each domain. Each virtual host file should have a unique ServerName directive. After creating each configuration file, enable it with a2ensite and restart Apache: Apache will serve the correct content based on the domain name in the request, using the most specific matching virtual host configuration. For basic web hosting, the most commonly used modules include: You can check which modules are currently enabled with: To enable a module, use sudo a2enmod module_name, then restart Apache. If Apache fails to start after making configuration changes, follow these steps: Check the configuration syntax: Check Apache error logs for specific error messages: Check the systemd service status for detailed error information: If you recently enabled a module or site, try disabling it temporarily: The most common issues include syntax errors in configuration files, port conflicts, or missing required modules. Several configuration adjustments can improve Apache performance: Adjust the MPM settings: If using the event MPM (default on Ubuntu/Debian), you can tune MaxRequestWorkers, ThreadsPerChild, and ServerLimit in /etc/apache2/mods-available/mpm_event.conf. Enable compression: Enable the deflate module to compress responses: Optimize KeepAlive settings: In apache2.conf, adjust KeepAlive, MaxKeepAliveRequests, and KeepAliveTimeout based on your traffic patterns. Enable browser caching: Use the expires module to set appropriate cache headers for static content. Disable unnecessary modules: Remove unused modules to reduce the memory footprint: Always test performance changes and monitor your server’s resource usage after making adjustments. Apache is versatile and very modular, so configuration needs will be different depending on your setup. After reviewing some general use cases above, you should have a good understanding of what the main configuration files are used for and how they interact with each other. If you need to know about specific configuration options, the provided files are well commented and Apache provides excellent documentation. The configuration files should not be as intimidating now and you should feel more comfortable experimenting and modifying them to suit your needs. To continue learning about Apache and web server management, explore these related DigitalOcean tutorials: How To Secure Apache with Let’s Encrypt on Ubuntu: Learn how to secure your Apache server with free SSL/TLS certificates from Let’s Encrypt. How To Secure Apache with Let’s Encrypt on Debian: Step-by-step guide for securing Apache on Debian with SSL certificates. How To Install the Apache Web Server on Ubuntu: Complete installation guide for Apache on Ubuntu. Initial Server Setup Guide: Essential security and configuration steps for new Ubuntu and Debian servers. Ready to deploy your web applications? DigitalOcean Droplets provide scalable virtual machines perfect for hosting Apache web servers. With simple pricing, global data centers, and comprehensive documentation, DigitalOcean makes it easy to get your applications online quickly. For more advanced deployment needs, explore DigitalOcean App Platform, which automates infrastructure management and scaling so you can focus on building your applications. Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about our products Former Senior Technical Writer at DigitalOcean, specializing in DevOps topics across multiple Linux distributions, including Ubuntu 18.04, 20.04, 22.04, as well as Debian 10 and 11. I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix This textbox defaults to using Markdown to format your answer. You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link! I wonder why not tell people they can get rid of the warning about the Servers domain name just by adding one line to any of the apache config files. (i use the one that comes empty) The error that reads “[warning]: Could not determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName” Just run:
sudo nano /etc/apache2/httpd.conf Then add:
ServerName YOUR.IP.ADDRESS.HERE This tutorial assumes everyone is using the MPM worker module. What about MPM Prefork which is default in Ubuntu? Ah no it doesnt. In fact some MPM configuration options would be a good addition to this tutorial. Under directory definitions you have an error in the sample. It should end with </Directory>. Just an FYI Uhh… ok, so no tags. Lets just say your Directory tag doesn’t close? Hows that. @mike.voermans: Tags are stripped off, you can type > by replacing it with <strong>&</strong><strong>gt;</strong> and < by replacing it with <strong>&</strong><strong>gt;</strong>. I had to add a slash to </Directory> otherwise Apache complained… @Ian Jones: Right. Thanks for catching that! I’ve fixed it in the article. Er…o.O What does what do when there is no “default” file? FYI, I see 000-default.conf and default-ssl.conf. Inside of those one sees the “Virtualhost…” no <Directory /> … </Directory> sections. Okay, http://askubuntu.com/questions/386382/cannot-find-etc-apache2-sites-available-default-when-configuring-apachesays the 00-default.conf is the new filename. The configuration settings therein, however, are also different from this guide. You may want to update as notification to everyone that this won’t work for 14.04. ;) cd /etc/apache2
ls -F I have not the httpd.conf Please complete your information! Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation. Full documentation for every DigitalOcean product. The Wave has everything you need to know about building a business, from raising funding to marketing your product. Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter. New accounts only. By submitting your email you agree to our Privacy Policy Scale up as you grow — whether you're running one virtual machine or ten thousand. Sign up and get $200 in credit for your first 60 days with DigitalOcean.* *This promotional offer applies to new accounts only.