VPS & Server Khusus (Pusat Kontrol Hosting) Bantuan

How to Setup an Apache Virtual Host on CentOS 6

This article will show you how to configure a basic Apache Virtual Hosts. If you have a control panel, typically you wont need to follow these steps - but if you would like to learn how to create a website using command line only, this is your guide.
DIFFICULTY Basic - 1 | Medium - 2 | Advanced - 3
TIME REQUIRED 15-30+ min
RELATED PRODUCTS Linux-based VPS or dedicated servers

First, we need to create the document root for the virtual host we are about to create. For this example we will use virtualhost.com as the domain but you can use one of your own if you have one availible for this project. We will be logged in as root to complete these commands, permissions will be updated as we progress through the setup.

If this is one of many domains you plan on adding to the server, you want to make sure you create a consistent directory structure that you are able to replicate for other domains.

mkdir -p /var/www/virtualhost.com/public_html/

virtualhost.com being the domain you selected for this project.

Then we need to create an index file to validate whether or not we setup the Virtual Host correctly.

vi /var/www/virtualhost.com/public_html/index.html

Here is a snippet of code you can use to test your index file.

Virtual Host Is Working

You Have Successfully Configured an Apache Virtual Host on CentOS

Once we have saved that, then we need to run a few commands to ensure that the anonymous visitor from the internet can see your web content. First we need to update the ownership for our website.

chown -R user:user /var/www/virtualhost.com/

user being a non-root user on your system.

Now we need to fix the permissions to allow our www to be visible by the web.

chmod -R 755 /var/www/

That’s it, we have created the foundation in which our domains website content to be served from. Now we need to let Apache know where to serve this content and under what address. On CentOS, we will be modifying the Apache configuration directly, first open the httpd.conf and scroll to the bottom.

vi /etc/httpd/conf/httpd.conf

Scroll to the bottom, you will see a section call Virtual Hosts

NameVirtualHost *:80
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.

ServerAdmin address@personalemail.com
DocumentRoot /var/www/virtualhost.com/public_html
ServerName www.virtualhost.com
ServerAlias virtualhost.com
ErrorLog /var/www/virtualhost.com/error.log
CustomLog /var/www/virtualhost.com/requests.log

In this file, we will update a few things - in the example above we have already made these changes.

ServerAdmin - This is your email address, any apache notifications for this website will be sent to this address.

DocumentRoot - This is the document root we created in the beginning of this project, this variable tells Apache where the content is that needs to be served.

ServerName & ServerAlias - This is your domain name with and without the www. as you can see in the example above.

ErrorLog & CustomLog - These are the log locations for your domain name, by filling these in - you can easily parse through error logs on different domains.

Finally, we need to restart Apache for these changes to take effect.

/etc/init.d/httpd restart

There you have it, you have successfully create an Apache Virtual Host and can now visit your website on the internet. If you used a real domain, you need to update your DNS to point at your hosted server.

Apakah Artikel Ini Berguna?
Terima kasih atas masukan Anda. Untuk berbicara dengan perwakilan layanan pelanggan, hubungi nomor ponsel dukungan atau gunakan opsi chatting di atas.
Senang bisa membantu! Ada hal lain yang dapat kami lakukan untuk Anda?
Maaf tentang hal tersebut. Sampaikan apa yang membuat Anda bingung atau mengapa solusi tersebut tidak mengatasi masalah Anda.