The fastest way to comment out multiple lines
In Apache config files, you comment out a single line by placing a hash symbol (#) at the start. To comment out multiple lines at once, most text editors let you select the block and add the hash to every line in one action. In nano, select your lines and press Ctrl+Shift+4. In vi or vim, position your cursor on the first line, type :.,+Ncomment (where N is the number of lines), then press Enter. In VS Code or most graphical editors, select the lines and press Ctrl+/ to toggle comments on the whole block.
If your editor doesn't have a built-in comment feature, you can use sed from the command line. Open a terminal in the directory holding your Apache config file and run sed -i '5,10s/^/#/' apache2.conf to comment out lines 5 through 10 (replace the numbers and filename with your own). The ^ means "at the start of the line" and the # is what gets inserted there.
Always test your config after commenting lines out. Run apache2ctl configtest (on Debian and Ubuntu) or apachectl configtest (on Red Hat, CentOS, and macOS) to check for syntax errors before you restart Apache.
Key Takeaways
- Most text editors have a keyboard shortcut to comment out a selected block: Ctrl+/ in VS Code, Ctrl+Shift+4 in nano, or :.,+Ncomment in vim.
- From the command line, sed can comment out a range of lines in one command without opening the file in an editor.
- Always run apache2ctl configtest or apachectl configtest after commenting out lines to catch syntax errors before restarting the service.
- A commented line starts with # and is ignored by Apache, so the directive has no effect but remains in the file for reference.
- Commenting out is safer than deleting because you can restore the line by removing the # if you change your mind.
Using nano to comment out a block
Open your Apache config file in nano: nano /etc/apache2/apache2.conf (or the path to your config). Use the arrow keys to position your cursor at the start of the first line you want to comment out. Hold Shift and press the down arrow to select multiple lines, or use Shift+Ctrl+End to select to the end of the file.
Once your lines are highlighted, press Ctrl+Shift+4. Nano will add a # to the beginning of every selected line. If you make a mistake, press Ctrl+Z to undo. When you're done, press Ctrl+X to exit, then Y to save and Enter to confirm the filename.
Using vim or vi to comment out a block
Open your config file: vim /etc/apache2/apache2.conf. Move your cursor to the first line you want to comment. Press Escape to enter command mode (if you're not already there), then type :.,+4comment to comment out the current line plus the next 4 lines (5 lines total). Change the number 4 to however many lines below the current one you want to include.
Alternatively, you can select lines visually. Press Escape, then position your cursor on the first line. Press V to enter visual line mode, then use the arrow keys to select down to the last line you want. Type :comment and press Enter. Vim will add # to every selected line. Type :wq and press Enter to save and exit.
Using sed from the command line
If you prefer not to open an editor, sed can comment out lines without interactive editing. The basic syntax is sed -i 'START,ENDcommand' filename. To comment out lines 12 through 18 in your Apache config, run:
sed -i '12,18s/^/#/' /etc/apache2/apache2.conf
The -i flag tells sed to edit the file in place (it modifies the original). The range 12,18 specifies which lines to affect. The s/^/#/ part means "substitute at the start of the line (^) with a #". Always make a backup first: cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak.
Using VS Code or graphical editors
Open your Apache config file in VS Code or another graphical editor. Click at the start of the first line you want to comment, then hold Shift and click at the end of the last line. The entire block will be highlighted. Press Ctrl+/ (or Cmd+/ on macOS) to toggle comments. VS Code will add # to the start of every line in the selection.
If Ctrl+/ doesn't work, check your editor's menu. In VS Code, go to Edit > Toggle Line Comment. In other editors, look under Edit or a similar menu for "Comment" or "Toggle Comment". Most modern editors support this feature with a keyboard shortcut shown in the menu.
Testing your config after commenting lines
After you comment out lines, always test the syntax before restarting Apache. On Debian, Ubuntu, and derivatives, run apache2ctl configtest. On Red Hat, CentOS, Fedora, and derivatives, run apachectl configtest. On macOS, use apachectl configtest. The output will say "Syntax OK" if there are no errors, or it will show the line number and error message if something is wrong.
If the test passes, you can safely restart Apache. On systemd systems, run sudo systemctl restart apache2 (Debian/Ubuntu) or sudo systemctl restart httpd (Red Hat/CentOS). On older systems, use sudo service apache2 restart or sudo service httpd restart. If the test fails, undo your changes and review the commented lines for typos or syntax issues.
Uncommenting lines when you need them back
To remove the # and restore a commented line, use the same tools in reverse. In nano, select the lines and press Ctrl+Shift+4 again to toggle the comments off. In vim, position your cursor on a commented line, type :.,+Nuncomment (where N is the number of lines), and press Enter. In sed, use sed -i '12,18s/^#//' /etc/apache2/apache2.conf to remove the # from lines 12 through 18.
In VS Code and most graphical editors, select the commented lines and press Ctrl+/ to toggle them back to active code. Always test the config again after uncommenting to make sure the directives are valid and don't conflict with other settings.
Frequently Asked Questions
Will commenting out a line stop it from working immediately?
No. Apache only reads the config file when it starts or when you reload it. After you comment out lines, you must restart or reload Apache for the change to take effect. Run sudo systemctl restart apache2 (or httpd on Red Hat systems) to apply the change.
Can I comment out lines in the middle of a directive block?
Yes, but be careful. If you comment out part of a block that spans multiple lines, the remaining lines may cause a syntax error. For example, if you comment out the opening <Directory> tag but leave the closing </Directory>, Apache will fail to parse the file. Always comment out complete blocks or single directives, not partial ones.
What if sed doesn't work or gives an error?
Make sure you have the correct line numbers and that the file path is right. Run cat -n /etc/apache2/apache2.conf to see line numbers. If sed says "command not found", it's not installed (rare on Linux). If you get a permission error, use sudo before the sed command. Always test with a backup copy first.
Is it better to comment out or delete unused directives?
Commenting out is better. It preserves the line for reference, lets you restore it quickly if you change your mind, and keeps a record of what was there. Deleting removes that history. For production configs, commenting out is the safer choice.