{"id":16137,"date":"2023-06-22T11:29:21","date_gmt":"2023-06-22T11:29:21","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=16137"},"modified":"2023-07-05T11:50:02","modified_gmt":"2023-07-05T11:50:02","slug":"how-to-change-apache-http-and-https-ports-in-linux-ubuntu-centos","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-change-apache-http-and-https-ports-in-linux-ubuntu-centos\/","title":{"rendered":"How to Change Apache HTTP and HTTPS Ports in Linux (Ubuntu\/CentOS)"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-1024x768.jpg\" alt=\"How to Change Apache HTTP and HTTPS Ports in Linux (Ubuntu_CentOS)\" width=\"1024\" height=\"768\" class=\"alignnone size-large wp-image-16138 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-1024x768.jpg 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-300x225.jpg 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-1536x1152.jpg 1536w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-2048x1536.jpg 2048w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-128x96.jpg 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-420x315.jpg 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-540x405.jpg 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-720x540.jpg 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-960x720.jpg 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-1140x855.jpg 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-1320x990.jpg 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Change-Apache-HTTP-and-HTTPS-Ports-in-Linux-Ubuntu_CentOS-1440x1080.jpg 1440w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/768;\" \/><\/p>\n<p><a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a> has established itself as a reliable and robust solution. It&#8217;s one of the <a href=\"https:\/\/webhostinggeeks.com\/best\/web-server\/\">best web servers<\/a> available, powering a significant portion of the internet. This tutorial will guide you through the process of changing the HTTP and HTTPS ports for Apache on Linux distributions, specifically Ubuntu and CentOS.<\/p>\n<p>Why would you want to change these ports? There could be several reasons. Perhaps you&#8217;re running multiple web servers on a single machine, like <a href=\"https:\/\/webhostinggeeks.com\/blog\/nginx-server-explained\/\">Nginx<\/a> or <a href=\"https:\/\/webhostinggeeks.com\/blog\/litespeed-web-server-explained\/\">LiteSpeed<\/a>, and you need to avoid port conflicts. Or maybe you&#8217;re enhancing your server&#8217;s security by using non-standard ports to reduce the risk of automated attacks.<\/p>\n<p>No matter your reasons, this guide will provide a step-by-step process to help you achieve your goal. We&#8217;ll cover both Ubuntu and CentOS, as the process differs slightly between these two popular Linux distributions.<\/p>\n<h2>Prerequisites<\/h2>\n<p>Before we begin, ensure that you have the following:<\/p>\n<ul>\n<li>A Linux server running Ubuntu or CentOS. This could be a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server<\/a>, a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS server<\/a>, or even a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud hosting<\/a> or <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-shared-hosting\/\">shared hosting<\/a> environment, as long as you have root access or sufficient privileges.<\/li>\n<li>Apache installed and running on your server.<\/li>\n<li>Access to a terminal or command line interface, and basic knowledge of Linux commands.<\/li>\n<\/ul>\n<h2>Changing Apache HTTP\/HTTPS Ports in Ubuntu<\/h2>\n<p>Here are the steps to change the Apache HTTP and HTTPS ports in Ubuntu:<\/p>\n<h3>Step 1: Open the Apache Configuration File<\/h3>\n<p>The first step is to open the Apache configuration file. In Ubuntu, this file is typically located at \/etc\/apache2\/ports.conf. Use the nano text editor (or your preferred text editor) to open this file:<\/p>\n<pre>sudo nano \/etc\/apache2\/ports.conf<\/pre>\n<h3>Step 2: Modify the HTTP and HTTPS Ports<\/h3>\n<p>In the ports.conf file, you&#8217;ll see lines that specify the ports Apache listens on. They usually look like this:<\/p>\n<pre>\r\nListen 80\r\nListen 443\r\n<\/pre>\n<p>Change the numbers 80 and 443 to your desired HTTP and HTTPS ports, respectively. For example, if you want to change the HTTP port to 8080 and the HTTPS port to 8443, the lines should look like this:<\/p>\n<pre>\r\nListen 8080\r\nListen 8443\r\n<\/pre>\n<p>Save and close the file.<\/p>\n<h3>Step 3: Update the Virtual Host Files<\/h3>\n<p>Next, you need to update the virtual host files for any websites hosted on your server.<\/p>\n<p>These files are usually located in the \/etc\/apache2\/sites-available\/ directory. Each file represents a different website, and they contain directives that apply only to the specific website. Open each file with the nano text editor:<\/p>\n<pre>\r\nsudo nano \/etc\/apache2\/sites-available\/your_website.conf\r\n<\/pre>\n<p>In each file, you&#8217;ll see a <VirtualHost> block that looks something like this:<\/p>\n<pre>\r\n&lt;VirtualHost *:80&gt;\r\n    ServerAdmin webmaster@localhost\r\n    DocumentRoot \/var\/www\/html\r\n    ErrorLog ${APACHE_LOG_DIR}\/error.log\r\n    CustomLog ${APACHE_LOG_DIR}\/access.log combined\r\n&lt;\/VirtualHost&gt;\r\n<\/pre>\n<p>The number after the colon in the <VirtualHost> tag is the port number. Change it to the new HTTP port you specified in the ports.conf file. If there&#8217;s a <VirtualHost> block for HTTPS (port 443), change that to the new HTTPS port as well.<\/p>\n<p>Save and close each file when you&#8217;re done.<\/p>\n<h3>Step 4: Restart Apache<\/h3>\n<p>After making these changes, you need to restart Apache for the changes to take effect:<\/p>\n<pre>\r\nsudo systemctl restart apache2\r\n<\/pre>\n<p>You can check the status of the Apache service with:<\/p>\n<pre>\r\nsudo systemctl status apache2\r\n<\/pre>\n<p>If everything is set up correctly, Apache should restart without any errors.<\/p>\n<h2>Changing Apache HTTP\/HTTPS Ports in CentOS<\/h2>\n<p>The process for CentOS is similar to Ubuntu, but there are some differences in file locations and commands. Here are the steps:<\/p>\n<h3>Step 1: Open the Apache Configuration File<\/h3>\n<p>In CentOS, the main Apache configuration file is located at \/etc\/httpd\/conf\/httpd.conf. Open this file with the nano text editor:<\/p>\n<pre>\r\nsudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/pre>\n<h3>Step 2: Modify the HTTP and HTTPS Ports<\/h3>\n<p>In the httpd.conf file, look for lines that specify the Listen directive. These lines tell Apache to listen on the specified ports. Change the numbers to your desired HTTP and HTTPS ports, just like in Ubuntu.<\/p>\n<p>Save and close the file.<\/p>\n<h3>Step 3: Update the Virtual Host Files<\/h3>\n<p>In CentOS, the virtual host files are usually located in the \/etc\/httpd\/conf.d\/ directory. Each file represents a different website. Open each file and change the port numbers in the <VirtualHost> blocks, just like in Ubuntu.<\/p>\n<h3>Step 4: Restart Apache<\/h3>\n<p>Finally, restart Apache for the changes to take effect:<\/p>\n<pre>\r\nsudo systemctl restart httpd\r\n<\/pre>\n<p>You can check the status of the Apache service with:<\/p>\n<pre>\r\nsudo systemctl status httpd\r\n<\/pre>\n<p>If everything is set up correctly, Apache should restart without any errors.<\/p>\n<h2>Frequently Asked Questions<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What happens if I forget to update my firewall rules after changing the Apache ports?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<span itemprop=\"text\">If you forget to update your firewall rules after changing the Apache ports, your server may not respond to HTTP or HTTPS requests on the new ports. This is because the firewall may still be blocking incoming connections on these ports. To fix this, you need to update your firewall rules to allow traffic on the new ports.<\/span>\n<\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Can I use any number for the HTTP and HTTPS ports?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<span itemprop=\"text\">You can use any number between 1024 and 65535 for the HTTP and HTTPS ports, as long as they are not being used by another service. Ports below 1024 are reserved for well-known services and require root privileges to bind to.<\/span>\n<\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Can I use different ports for different websites on the same server?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<span itemprop=\"text\">Yes, you can use different ports for different websites on the same server. This is done by specifying different ports in the <VirtualHost> blocks in the virtual host files for each website. However, remember that if you use non-standard ports, visitors will have to append the port number to the URL in their web browser.<\/span>\n<\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What should I do if Apache fails to restart after changing the ports?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<span itemprop=\"text\">If Apache fails to restart after changing the ports, check the Apache error logs for any error messages. The error logs are usually located at \/var\/log\/apache2\/error.log in Ubuntu and \/var\/log\/httpd\/error_log in CentOS. The error messages can give you clues about what went wrong.<\/span>\n<\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Is it necessary to change the Apache ports for security reasons?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<span itemprop=\"text\">Changing the Apache ports can enhance security by reducing the risk of automated attacks that target the default HTTP and HTTPS ports. However, it&#8217;s not a comprehensive security solution. You should still implement other security measures, such as keeping your server software up-to-date, using strong passwords, and configuring a firewall.<\/span>\n<\/p>\n<\/li>\n<\/ol>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">sudo nano \/etc\/apache2\/ports.conf<\/span> \u2013 Opens the Apache ports configuration file in Ubuntu.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl restart apache2<\/span> \u2013 Restarts the Apache service in Ubuntu.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl status apache2<\/span> \u2013 Checks the status of the Apache service in Ubuntu.<\/li>\n<li><span class=\"fw-bold\">sudo nano \/etc\/httpd\/conf\/httpd.conf<\/span> \u2013 Opens the Apache configuration file in CentOS.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl restart httpd<\/span> \u2013 Restarts the Apache service in CentOS.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl status httpd<\/span> \u2013 Checks the status of the Apache service in CentOS.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Changing the HTTP and HTTPS ports in Apache is a common task for web server administrators. Whether you&#8217;re running a single website or managing multiple sites on a single server, understanding how to adjust these settings is crucial. This guide has provided you with step-by-step instructions on how to change these ports on both Ubuntu and CentOS systems.<\/p>\n<p>Remember, while changing these ports can help avoid conflicts with other services and potentially increase security, it&#8217;s not a standalone solution for securing your server. Always ensure you&#8217;re following best practices for server security, including regular updates, strong authentication measures, and appropriate firewall configurations.<\/p>\n<p>By following this guide, you&#8217;ve taken another step in enhancing your skills as a web server administrator. Keep exploring, keep learning, and continue to make the most out of your Apache web server setup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache has established itself as a reliable and robust solution. It&#8217;s one of the best web servers available, powering a significant portion of the internet. This tutorial will guide you&#8230;<\/p>\n","protected":false},"author":6,"featured_media":16138,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1103,2055,1073],"tags":[1199,1244,1856],"class_list":["post-16137","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache","category-centos","category-ubuntu","tag-apache","tag-centos","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/16137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=16137"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/16137\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/16138"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=16137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=16137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=16137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}