Configure Apache Virtual Hosts-centos 7

Source: Internet
Author: User

Difficulty: 2
Time: minutes

Want to host websites on your server? Using Apache? Great. This article would show you what to do exactly that using Apache's "virtual hosts."

In Apache, you can use virtual hosts to direct http traffic-a given domain name to a particular directory (i.e. the R Oot Directory of the website for the domain in the request). This feature are commonly used to host multiple websites, and we recommend using it for every website on your server in Cluding the first.

Throughout this article, we'll use a example domain- coolexample.com -but you should replace it with the Domai n Name or subdomain want to host on your server.

Install the Apache Web server

To get Apache on your server, you can either install it as part of a LAMP stack, or you can install the Apache by itself:

    1. Update your packages using yum : sudo yum update
    2. Install Apache:sudo yum Install httpd
    3. Start up Apache, so, the httpd service would start automatically on a Reboot:sudo service httpd start
Set up the virtual host
    1. Create the virtual directories for your Domain:sudo mkdir-p/var/www/coolexample.com/public_html
    2. Change the ownership to the Apache Group:sudo chown-r apache:apache/var/www/coolexample.com/public_htmlthis le TS Apache Modify files in your web directories.
    3. Change the directory ' s permissions so they can is read from the Internet:sudo chmod-r 755/var/www/
Create Content for the website

If you had the content for the website prepped, you can upload it to the folder of the last section of you /public_html created.

If you don't have content ready to upload, you can create a sample home page (also known as an index file, which is the fi RST page that loads when visitors come to your domain).

    1. Create the index File:sudo vim/var/www/coolexample.com/public_html/index.html
    2. Add some content to the file:
    3. Save and close the file::wq!
Configure Your Virtual host directories

We ' re going to copy a configuration usually used in Ubuntu/debian and create both Directories:one to store the virtual hos T files ( sites-available ) and another to the symbolic links to virtual hosts, that'll be published ( sites-enabled ).

Create sites-available and sites-enabled directories
    • Create the Directories:sudo Mkdir/etc/httpd/sites-availablesudo mkdir/etc/httpd/sites-enabled
Edit your Apache configuration file

Edit The main configuration file () So, Apache would look for httpd.conf virtual hosts in the sites-enabled directory.

    1. Open your config File:sudo vim/etc/httpd/conf/httpd.conf
    2. Add this on the very end of the file:includeoptional sites-enabled/*.confthis, we ' re telling Apache to a dditional config files in the sites-enabled directory.
    3. Save and close the file::wq!
Create Virtual Host File

We ' re going to build it from a new file in your sites-available directory.

    1. Create a new config File:sudo vim/etc/httpd/sites-available/coolexample.com.conf
    2. Paste this code in, replacing your own domain for coolexample.com.conf.

      Here's what's the whole file could look like after your changes:

      <virtualhost *:80>    ServerAdmin [email protected]        ServerName www.coolexample.com    Serveralias coolexample.com     documentroot/var/www/coolexample.com/public_html     errorlog/var/www/coolexample.com/ Error.log     Customlog/var/www/coolexample.com/requests.log Combined </VirtualHost>

The lines ErrorLog CustomLog and is not required to set up your virtual host, but we ' ve included them Tell the Apache where to keep the error and request logs for your site.

    • Save and close the file::wq!
    • Enable your virtual host file with a sym link to the sites-enabled directory:sudo ln-s/etc/httpd/sites-available/coolexample . com. conf/etc/httpd/sites-enabled/coolexample.com. conf
    • Restart Apache:sudo Service httpd Restart
Point your domain name to your server

If your domain name isn ' t currently loading another website, you should point it to your server to test your new config.

How do I depends on where your domain name is registered and whose server you ' re using:

Do this
Domain registered? Server hosted?...
Godaddy Godaddy Point your domain name to a server
Another company Godaddy Find a server ' s public IP address and then update your domain name ' s primary ("@") a record.
Godaddy Another company Find Your server ' s IP address, and then change your domain's IP address to use it.
Another company Another company Find Your server ' s IP address, and then change your domain's IP address to use it.

Changes to your domain can take up to hours to display across the Internet. However, once they do, you can visit your domain name and view the test page you created earlier!

Adding Additional virtual hosts

To create additional sites, repeat the following sections:

    1. Set up the virtual host
    2. Create Content for the website
    3. Create virtual host File-but For additional virtual hosts, you'll need to the Create new config files /etc/httpd/sites-available/ in, for Exampl e:/etc/httpd/sites-available/Your second domain name
    4. Point your domain name to your server

Configure Apache Virtual Hosts-centos 7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.