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:
- Update your packages using
yum:sudo yum update - Install Apache:
sudo yum install httpd
- Start up Apache, so that the httpd service will start automatically on a reboot:
sudo service httpd start
Set up the virtual host
- Create the virtual directories for your domain:
sudo mkdir -p /var/www/coolexample.com/public_html
- 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.
- 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).
- Create the index file:
sudo vim /var/www/coolexample.com/public_html/index.html
- 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> - 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-availablesudo 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.
- Open your config file:
sudo vim /etc/httpd/conf/httpd.conf
- 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-enableddirectory. - 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.
- Create a new config file:
sudo vim /etc/httpd/sites-available/coolexample.com.conf
- 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-enableddirectory: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:
- Set up the virtual host
- Create content for the website
- 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 - Point your domain name to your server
Configure Apache Virtual Hosts - CentOS 7的更多相关文章
- How To Set Up Apache Virtual Hosts on CentOS 6
About Virtual Hosts 虚拟主机,用于在一个单一IP地址上,运行多个域.这对那些想在一个VPS上,运行多个网站的人,尤其有用.基于用户访问的不同网站,给访问者显示不同的信息.没有限制能 ...
- [转]Windows 下 Apache Virtual hosts 简单配置
From : http://blog.csdn.net/wuerping/article/details/4164362 /* Author : Andrew.Wu [ Created on : 20 ...
- Virtual Box + CentOS Minimal + Apache搭建Web服务器
本文并不介绍关于Virtual Box, CentOS, Apache的安装, 主要针对安装后相关的配置, 使宿主机(Host)可以访问客户机(Guest: CentOS in Virtual Box ...
- #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache
#Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...
- Building Apache Thrift on CentOS 6.5
Building Apache Thrift on CentOS 6.5 Starting with a minimal installation, the following steps are r ...
- 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-v ...
- Apache配置基于端口号的虚拟主机 Apache virtual host configuration is based on the port
有可能只有一个ip出口,但却有多个项目,那么就需要基于端口号架设虚拟主机. Step 1: 检查是否开启 httpd-vhosts.conf apache/conf/httpd.conf文件 # Vi ...
- [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...
- Install and Configure Apache Kafka on Ubuntu 16.04
https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hi ...
随机推荐
- (转)[BetterExplained]为什么你应该(从现在开始就)写博客
(一)为什么你应该(从现在开始就)写博客 用一句话来说就是,写一个博客有很多好处,却没有任何明显的坏处.(阿灵顿的情况属于例外,而非常态,就像不能拿抽烟活到一百岁的英国老太太的个例来反驳抽烟对健康的极 ...
- Unable to make the session state request to the session state server处理
Server Error in '/' Application. Unable to make the session state request to the session state serve ...
- 为什么导入数据库要加入set names utf-8
Repinted:http://blog.csdn.NET/class1/archive/2006/12/30/1469298.aspx 为了让你的网页能在更多的服务器上正常地显示,还是加上" ...
- 007_Web to lead
- ReactiveCocoa的一些使用
作为一名iOS开发者, 你写的每一行代码几乎都是对某些事件的反馈:点击button.接收到网络信息. 一个属性的改变(通过KVO监测) 或者 通过CoreLocation监听用户所在位置的变化 以上等 ...
- MVC5路由系统机制详细讲解
请求一个ASP.NET mvc的网站和以前的web form是有区别的,ASP.NET MVC框架内部给我们提供了路由机制,当IIS接受到一个请求时,会先看是否请求了一个静态资源(.html,css, ...
- ==与equals 的使用比较
1. == 是一个运算符. 2.Equals则是string对象的方法 我们通常是两种类型的比较 1.基本数据类型比较 2.引用对象比较 其中 1.基本数据类型比较 ==和Equals都比较两个值是否 ...
- ftp相关资料
一.ftp状态码 110 重新启动标记应答.在这种情况下文本是确定的,它必须是:MARK yyyy=mmmm,其中yyyy是用户进程数据流标记,mmmm是服务器标记. 120 ...
- [Linux系统]查看内存的几种方式
1. cat /proc/meminfo 2.free -m3.vmstat -s4.ps命令可以实时的显示各个进程的内存使用情况.Reported memory usage information ...
- Winform MDI窗体容器,权限以及简单通讯
MDI窗体容器: 一般来说,窗体是顶级容器,不允许放在其他任何容器内,但是如果将某个窗体的IsMdiContainer属性设置为True,那此窗体就会成为窗体容器,可以在其中放入其他窗体 在内部的窗体 ...