The fastest way to disable a block of settings in Apache
To comment out multiple lines in an Apache config file, select the lines you want to disable, then add a # character at the start of each one. Apache treats any line beginning with # as a comment and ignores it when the server starts. This is safer than deleting the lines — you can see what was disabled and turn it back on later without retyping.
The method you use depends on what tool you have open. A text editor with find-and-replace can do this in seconds. If you are editing directly in the terminal, you can use sed (a command-line text tool) to add # to multiple lines at once. Both approaches leave the original text intact and readable.
Key Takeaways
- Apache ignores any line that starts with #, so commenting out lines disables them without deleting them.
- In a text editor, you can select multiple lines and use find-and-replace to add # to the start of each one.
- From the command line, the sed command can add # to a range of lines in one operation.
- Always test your config file with apache2ctl configtest (or apachectl configtest on some systems) after making changes to catch syntax errors before restarting.
- Keep a backup of your config file before making bulk changes, so you can restore it if something goes wrong.
Using find-and-replace in a text editor
Most text editors — including VS Code, Sublime Text, Nano, and Vim — have a find-and-replace feature that works across multiple lines. Open your Apache config file (usually located at /etc/apache2/apache2.conf on Linux, or /etc/apache2/sites-available/ for individual site configs). Select the block of lines you want to comment out by clicking at the start of the first line and dragging to the end of the last line.
In VS Code or Sublime Text, press Ctrl+H (or Cmd+H on Mac) to open find-and-replace. In the "Find" field, type ^ (which means "start of a line"). Leave the "Replace" field empty, then click "Replace All" or use the replace button for each match. This adds nothing, so instead use a different approach: find each line's content and replace it with # plus that content. A simpler method is to select your lines, then use your editor's "Add prefix to lines" feature if it has one — VS Code calls this "Add Line Comment" and it is under the Edit menu.
In Nano, select the lines with Shift+Arrow keys, then there is no built-in prefix tool, so you will need to manually add # to each line or use sed from the command line instead. In Vim, position your cursor at the start of the first line you want to comment, then type :,+N s/^/#/ where N is the number of lines below the current one (so :,+5 s/^/#/ comments out 6 lines total). Press Enter and Vim will add # to each line.
Using sed to comment out a range of lines
If you are working in the terminal and want to comment out lines 10 through 20 of your config file, use sed with the -i flag to edit the file in place. The command is:
sed -i '10,20s/^/#/' /etc/apache2/apache2.conf
This tells sed to find lines 10 through 20, match the start of each line (^), and replace it with #. The file is changed when ready. If you want to be extra safe, first make a backup: cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.backup. Then run the sed command. If something goes wrong, you can restore with cp /etc/apache2/apache2.conf.backup /etc/apache2/apache2.conf.
You can also use sed to comment out lines that match a pattern instead of a line number range. For example, to comment out every line that contains the word "LoadModule", use:
sed -i '/LoadModule/s/^/#/' /etc/apache2/apache2.conf
This is useful when you want to disable a specific feature across multiple lines without having to count line numbers.
Uncommenting lines when you need them back
To reverse the process and uncomment lines, use sed again with a slightly different pattern. To uncomment lines 10 through 20:
sed -i '10,20s/^#//' /etc/apache2/apache2.conf
This removes the # from the start of each line in that range. To uncomment all lines containing a specific word, use:
sed -i '/LoadModule/s/^#//' /etc/apache2/apache2.conf
Be careful with this command — it will uncomment every matching line, even ones you may have commented out for a reason. If you are unsure, check the file first with grep "^#LoadModule" /etc/apache2/apache2.conf to see which lines would be affected.
Testing your config file after changes
After you comment out or uncomment lines, always test the config file before restarting Apache. Run:
apache2ctl configtest
On some systems, the command is apachectl configtest instead. If the syntax is correct, you will see "Syntax OK". If there is an error, the output will tell you which line has the problem and what is wrong. Fix the issue and test again before restarting the server with systemctl restart apache2 or service apache2 restart.
A common mistake is commenting out a line that another line depends on — for example, disabling a module that a VirtualHost directive needs. The config test will catch this and tell you which line is the problem, so you can uncomment what you need.
Keeping your config file readable
When you comment out a large block, add a note above it explaining why. For example:
# Disabled 2025-01-15: Old SSL config, replaced with modern ciphers below
This helps you and anyone else reading the file understand what was disabled and when. It also makes it easier to find and uncomment later. If you have many commented-out sections, consider moving them to a separate file instead — create a file like /etc/apache2/disabled-configs.conf and include it at the bottom of your main config with a comment explaining it is for reference only.
Keep your main config file focused on what is actually running. Commented-out code that has been there for months or years just adds clutter and makes it harder to troubleshoot problems.
Frequently Asked Questions
Will commenting out lines slow down Apache?
No. Apache reads the config file once when it starts, and it skips any line that begins with #. Commented-out lines have no effect on performance. However, a very large config file with thousands of lines (commented or not) can take slightly longer to parse at startup — but this is rarely noticeable.
Can I comment out lines in the middle of a directive?
No. Directives in Apache config files must be complete. If a directive spans multiple lines, you must comment out all of them or none of them. For example, if you have a <Directory> block that spans lines 15 to 25, you need to comment out all 11 lines, not just some of them. The config test will tell you if you have broken a directive.
What if I comment out the wrong lines by mistake?
If you have a backup, restore it with cp /etc/apache2/apache2.conf.backup /etc/apache2/apache2.conf. If you do not have a backup, use sed -i '/^#/!d' to see only the commented lines, or use your editor's undo feature if you have not closed the file yet. This is why making a backup before bulk changes is important.
Can I comment out lines in included config files the same way?
Yes. Apache includes other config files using the Include directive. You can comment out lines in any of those files using the same methods — sed, find-and-replace, or manual editing. Just remember to test the main config file afterward with apache2ctl configtest, because errors in included files will show up there.