Configure Apache Virtual Hosts - CentOS 7-阿里云开发者社区

开发者社区> 炉火纯青> 正文

Configure Apache Virtual Hosts - CentOS 7

简介: Difficulty: 2Time: 15 minutes Want to host websites on your server? Using Apache? Great. This article will show you how to do exactly that using Apache’s “virtual hosts.
+关注继续查看

Difficulty: 2
Time: 15 minutes

Want to host websites on your server? Using Apache? Great. This article will show you how to do exactly that using Apache’s “virtual hosts.”

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

Throughout this article, we'll use an example domain - coolexample.com - but you should replace it with the domain name or subdomain you 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 Apache by itself:

  1. Update your packages using yum:
    sudo yum update
  2. Install Apache:
    sudo yum install httpd
  3. Start up Apache, so that the httpd service will 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_html
    This lets Apache modify files in your web directories.
  3. Change the directory's permissions so they can be read from the internet:
    sudo chmod -R 755 /var/www/

Create content for the website

If you have the content for the website prepped, you can upload it to the /public_htmlfolder you created in the last section.

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 first 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:
    <html> 
        <head> 
        <title>Welcome to my site!</title> 
        </head> 
        <body> 
        <h1>Hooray! Your virtual host is working!</h1> 
        </body> 
    </html>
  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 two directories: one to store the virtual host files (sites-available) and another to hold symbolic links to virtual hosts that will be published (sites-enabled).

Create sites-available and sites-enabled directories

  • Create the directories:
    sudo mkdir /etc/httpd/sites-available
    sudo mkdir /etc/httpd/sites-enabled

Edit your Apache configuration file

Edit the main configuration file (httpd.conf) so that Apache will look for virtual hosts in the sites-enabled directory.

  1. Open your config file:
    sudo vim /etc/httpd/conf/httpd.conf
  2. Add this line at the very end of the file:
    IncludeOptional sites-enabled/*.conf
    This way, we're telling Apache to look for additional 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 the whole file could look like after your changes:

    <VirtualHost *:80>
        ServerAdmin webmaster@dummy-host.example.com    
        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 and CustomLog are not required to set up your virtual host, but we've included them, in case you do want to tell Apache where to keep 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 you do this depends on where your domain name is registered and whose server you're using:

Domain registered? Server hosted? Do this...
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 48 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 will need to create new config files in /etc/httpd/sites-available/, for example:
    /etc/httpd/sites-available/your second domain name
  4. Point your domain name to your server

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Virtual Pc 虚拟机不能全屏
解决方法:虚拟机分辨率改成跟主机屏幕一样的。 
712 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
4461 0
【转】Virtual 与 abstract 区别之我见
Virtual 与 abstract 区别之我见 原文:http://www.cnblogs.com/wyjgreat/archive/2011/06/07/2074182.html    1.abstract方法只能在抽象类中声明,虚方法则不是。
821 0
使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
7745 0
failed to create the java virtual machine
解决方法: 寻找eclipse解压路径下的eclipse.ini文件,使用写字板打开,将第10行和第14行的数据:原来位256,修改为128。即可!   解释原因: 虚拟机为java永久生成对象(Permanate generation)如,class对象、方法对象这些可反射(reflect...
517 0
+关注
炉火纯青
pangguoming.com
260
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载