Configure Apache Virtual Hosts - CentOS 7

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 [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 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
时间: 2024-10-10 06:28:35

Configure Apache Virtual Hosts - CentOS 7的相关文章

How To Set Up Apache Virtual Hosts on CentOS 6

About Virtual Hosts 虚拟主机,用于在一个单一IP地址上,运行多个域.这对那些想在一个VPS上,运行多个网站的人,尤其有用.基于用户访问的不同网站,给访问者显示不同的信息.没有限制能VPS中,添加的虚拟主机的个数. Set Up 教程中的这些步骤,需要你有root权限.你可以查看Initial Server Setup ,来学习如何设置初始化服务器.这里使用www.作为root的名字,你可以使用你想用的. 另外,你需要在你的VPS上安装并运行apache. 如果没有安装,你可以

Apache Virtual Hosting IP Based and Name Based Virtual Hosts

As we all are aware that Apache is a very powerful, highly flexible and configurable Web server for Nix OS. Here in this tutorial, we are going to discuss one more feature of Apachewhich allows us to host more than one website on a single Linux machi

Virtual Box + CentOS Minimal + Apache搭建Web服务器

本文并不介绍关于Virtual Box, CentOS, Apache的安装, 主要针对安装后相关的配置, 使宿主机(Host)可以访问客户机(Guest: CentOS in Virtual Box)提供的Web服务. Virtual Box网络设置 网上不少文章说要使用Host访问Guest的Web服务, 需要提供两种网络(NAT与HostOnly), 实际上我们只需要使用NAT网络即可达到目的. 以网卡1为例, 在连接方式中选择"网络地址转换(NAT)", 混杂模式设为"

Building Apache Thrift on CentOS 6.5

Building Apache Thrift on CentOS 6.5 Starting with a minimal installation, the following steps are required to build Apache Thrift on Centos 6.5. This example builds from source, using the current development master branch. These instructions should

Apache配置基于端口号的虚拟主机 Apache virtual host configuration is based on the port

有可能只有一个ip出口,但却有多个项目,那么就需要基于端口号架设虚拟主机. Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Virtual hosts Include conf/extra/httpd-vhosts.conf 如果没有开启,必须在httpd.conf文件中设置:如果开启,则可以在apache/conf/extra/httpd-vhosts.conf文件中设置,当然也还是可以再httpd.conf文件中进行设置

Apache配置基于IP的虚拟主机 Apache virtual host configuration is based on the IP

Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Virtual hosts Include conf/extra/httpd-vhosts.conf 如果没有开启,必须在httpd.conf文件中设置:如果开启,则可以在apache/conf/extra/httpd-vhosts.conf文件中设置,当然也还是可以再httpd.conf文件中进行设置,同样有效. Step 2: httpd.conf文件 DocumentRo

pxe+tftp+kickstart+dhcp+apache无人值守安装centos

一.安装环境 centos6.5  IP:192.168.100.11/24  GW:192.168.100.1 关闭iptables.selinux 二.需要安装组件 dhcp服务器 tftp服务器 kickstart生成的ks.cfg配置文件 提供安装镜像的httpd服务器 三.安装 1安装dhcp服务器       #yum install -y dhcp        #cp -a /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp

Install and Configure Apache Kafka on Ubuntu 16.04

https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hitjethvaon Oct 03, 2016 Intermediate Table of Contents Introduction Features Requirements Getting Started Installing Java Install ZooKeeper Install and

使用Dockerfile创建带Apache服务的Centos Docker镜像

在宿主机上准备的文件清单: Dockerfile #启动ssh和apache服务的角本 run.sh 以上文件都放到/root/apache_centos目录下 mkdir -p /root/apache_centos cd /root/apache_centos 基础镜像:以镜像centos为基础的开放SSH服务的镜像 [root@localhost apache_centos]# docker images REPOSITORY      TAG          IMAGE ID