VPS & Máy chủ chuyên dụng (Trung tâm kiểm soát dịch vụ lưu trữ) Trợ giúp

How to Setup a name-based virtual host on Fedora 20

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 e.
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 coolcoolexample.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/coolexample.com/html/

coolexample.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/coolexample.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/coolexample.com/

user being a non-root user on your system.

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 Fedora, we will be modifying the Apache configuration directly, first open the httpd.conf and scroll to the bottom.

vi /etc/httpd/conf/httpd.conf

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/coolcoolexample.com/public_html
ServerName www.coolcoolexample.com
ServerAlias coolcoolexample.com
ErrorLog /var/www/coolcoolexample.com/error.log
CustomLog /var/www/coolcoolexample.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.

Before restarting Apache, you can check your syntax by running the following command:

httpd -S

The output should have the following line, letting you know you added the virtual host correctly.

VirtualHost configuration: coolexample.com (/etc/httpd/conf/httpd.conf:357)

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 name-based 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.

Bài này có hữu ích không?
Thanks for your feedback. To speak with a customer service representative, please use the support phone number or chat option above.
Rất vui vì chúng tôi đã giúp được bạn! Chúng tôi có thể làm gì thêm cho bạn?
Rất tiếc về điều đó. Tell us what was confusing or why the solution didn’t solve your problem.