{"id":701,"date":"2019-11-13T08:13:55","date_gmt":"2019-11-13T08:13:55","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/uncategorized\/vpshow-to-create-apache-vhost-centos7\/"},"modified":"2025-04-23T09:21:26","modified_gmt":"2025-04-23T09:21:26","slug":"how-to-create-apache-vhost-on-centos","status":"publish","type":"post","link":"\/in\/tutorials\/how-to-create-apache-vhost-on-centos","title":{"rendered":"How to create Apache virtual hosts on CentOS 9 Stream"},"content":{"rendered":"<p>If you want to host more than one domain on your server, you need to create corresponding hosts on the webserver. That way, your server can deliver different content for different requests. In this tutorial, you will learn how to create Apache virtual hosts on CentOS 9 Stream.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet-1024x283.png\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2022\/11\/Linux-cheat-sheet-1536x425.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2022\/11\/Linux-cheat-sheet-300x83.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2022\/11\/Linux-cheat-sheet-150x41.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2022\/11\/Linux-cheat-sheet-768x212.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2022\/11\/Linux-cheat-sheet.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-1-installing-apache\">1. Installing Apache<\/h2><p>Before we begin, make sure that you have root access to your VPS or server using an <a href=\"\/in\/tutorials\/how-to-use-putty-ssh\">SSH <\/a>connection. If you&rsquo;re using Hostinger&rsquo;s hPanel, you will find your login credentials near the top of the <strong>Overview <\/strong>page.<\/p><figure data-wp-context='{\"imageId\":\"69e2029980400\"}' data-wp-interactive=\"core\/image\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" width=\"1810\" height=\"664\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\" alt=\"VPS Overview page with root access credentials near top of panel\" class=\"wp-image-125759\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2017\/01\/hostinger-vps-overview-screen-v2.png 1810w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2017\/01\/hostinger-vps-overview-screen-v2-300x110.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2017\/01\/hostinger-vps-overview-screen-v2-1024x376.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2017\/01\/hostinger-vps-overview-screen-v2-150x55.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2017\/01\/hostinger-vps-overview-screen-v2-768x282.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2017\/01\/hostinger-vps-overview-screen-v2-1536x563.png 1536w\" sizes=\"(max-width: 1810px) 100vw, 1810px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><ol class=\"wp-block-list\">\n<li>Install Apache on your CentOS 9 machine by typing the following command: <pre>sudo yum -y install httpd<\/pre><\/li>\n\n\n\n<li>Once the installation is completed, enable Apache as a CentOS service:\n<pre>sudo systemctl enable httpd.service<\/pre>\n<\/li>\n\n\n\n<li>Visit your server&rsquo;s IP address to check whether Apache is already running or not. The page should look like this:<br><img decoding=\"async\" class=\"size-large wp-image-22273 aligncenter\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2016\/11\/apache-welcome-page-1024x267.png\" alt=\"Apache testing page\" width=\"640\" height=\"167\" data-wp-editing=\"1\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2016\/11\/apache-welcome-page.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2016\/11\/apache-welcome-page-150x39.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2016\/11\/apache-welcome-page-300x78.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2016\/11\/apache-welcome-page-768x200.png 768w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/li>\n<\/ol><h2 class=\"wp-block-heading\" id=\"h-2-creating-a-directory-tree\">2. Creating a directory tree<\/h2><ol class=\"wp-block-list\">\n<li>A directory tree is used to hold website data. First, set the working directory to <strong>\/var\/www<\/strong> by running this command:\n<pre>cd \/var\/www\/<\/pre>\n<\/li>\n\n\n\n<li>You should use a unique document root for each virtual host:\n<pre>mkdir -p yourdomain.com\/public_html<\/pre>\n<p>Remember to replace <strong>yourdomain.com<\/strong> with your actual domain name.<\/p>\n<\/li>\n\n\n\n<li>Make the directory accessible to Apache. Run the <a href=\"\/in\/tutorials\/linux-chown-command\/\"><strong>chown<\/strong> command<\/a> to change the ownership and <strong>chmod<\/strong> to set correct permissions for the whole web directory. <pre>chown -R apache:apache \/var\/www\/yourdomain.com\/public_html chmod -R 755 \/var\/www<\/pre> <\/li>\n<\/ol><p>Apache now has the required access to create additional directories and serve content for incoming queries.<\/p><h2 class=\"wp-block-heading\" id=\"h-3-creating-a-demo-page\">3. Creating a demo page<\/h2><p>It is recommended that you make a demo page for your Apache virtual hosts. This way, you can check whether the host is working before you actually move your website files. Here&rsquo;s how you do it:<\/p><ol class=\"wp-block-list\">\n<li>Use the nano editor to create an <strong>index.html<\/strong> file in <strong>yourdomain.com\/public_html<\/strong> directory: <pre>nano yourdomain.com\/public_html\/index.html<\/pre><\/li>\n\n\n\n<li>Paste the following content to the file:\n<pre>&lt;html&gt;\n  &lt;head&gt;\n  &nbsp; &lt;title&gt;This is a test page&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n  &nbsp; &lt;h1&gt;It works!&lt;\/h1&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<\/li>\n\n\n\n<li>Save the file by pressing <strong>CTRL + X<\/strong> and then <strong>Y<\/strong>.<\/li>\n<\/ol><h2 class=\"wp-block-heading\" id=\"h-4-creating-the-virtual-host\">4. Creating the virtual host<\/h2><ol class=\"wp-block-list\">\n<li>Create a new virtual host <strong>.conf<\/strong> file in the Apache configuration directory:\n<pre>nano \/etc\/httpd\/conf.d\/yourdomain.com.conf<\/pre>\n<\/li>\n\n\n\n<li>Insert the following content into the .conf file:\n<pre>&lt;VirtualHost *:80&gt;\n&nbsp; &nbsp; ServerName www.yourdomain.com\n  &nbsp; ServerAlias yourdomain.com\n  &nbsp; DocumentRoot \/var\/www\/yourdomain.com\/public_html\n  &nbsp; ErrorLog \/var\/www\/yourdomain.com\/error.log\n  &nbsp; CustomLog \/var\/www\/yourdomain.com\/requests.log combined\n&lt;\/VirtualHost&gt;<\/pre>\n<p>In the example above, we tell Apache that we will be using <strong>port 80<\/strong> for the communication and that <strong>yourdomain.com<\/strong> is the name of the virtual host. Additionally, we also specify directories for the website files (document root) and error logs.<\/p>\n<\/li>\n\n\n\n<li>Restart Apache for the changes to take effect:\n<pre>systemctl restart httpd.service<\/pre>\n<\/li>\n<\/ol><p>And that&rsquo;s it &ndash; you have just created an Apache virtual host for your domain! Now try to access the host, and you should see the &ldquo;It works!&rdquo; text on the demo page we made earlier.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>You have learned how to create an Apache virtual host in four easy steps. To summarize, let&rsquo;s take a look at them once again:<\/p><ol class=\"wp-block-list\">\n<li>Install Apache from CentOS 9 Stream.<\/li>\n\n\n\n<li>Create a directory tree that will be used to hold all your website files.<\/li>\n\n\n\n<li>Make a demo page to see if the Apache virtual host is working properly.<\/li>\n\n\n\n<li>Create the virtual host by making configuration files in the Apache directory.<\/li>\n<\/ol><p>We hope you&rsquo;ve found this tutorial useful. Feel free to comment below if you have any questions!<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>If you want to host more than one domain on your server, you need to create corresponding hosts on the webserver. That way, your server can deliver different content for different requests. In this tutorial, you will learn how to create Apache virtual hosts on CentOS 9 Stream. 1. Installing Apache Before we begin, make [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/how-to-create-apache-vhost-on-centos\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[22642,22640],"tags":[],"class_list":["post-701","post","type-post","status-publish","format-standard","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-create-apache-vhost-on-centos","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-create-apache-vhost-on-centos","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=701"}],"version-history":[{"count":15,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":125764,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/701\/revisions\/125764"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}