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_html
folder 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-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.
- 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-enabled
directory. - 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-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:
- 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
- Tomcat配置Web程序几种方式
- java并发编程学习: 守护线程(Daemon Thread)
- HTML条件注释判断浏览器及检验
- HttpClient(二)HttpClient使用Ip代理与处理连接超时
- JavaScript中匿名函数的困惑
- 基础野:细说有符号整数
- Ubuntu14.04下如何开启Mysql远程访问
- docker学习(7) docker-compose使用示例
- docker学习(3) 容器的启动过程
- 基础野:细说原码、反码和补码
- JavaScript循环读书笔记
- docker学习(2) mac中docker-machine使用vmware fusion以及配置国内镜像加速
- HttpClient(一)HttpClient抓取网页基本信息
- Httpd运维日志:通过apxs添加模块
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Ceph 入门到实战之 RBD 块存储接口
- 聊聊 Python 面试最常被问到的几种设计模式(下)
- 带你用 Python 实现自动化群控(入门篇)
- 实战篇 | 基于freeRTOS的多任务事件传输demo(附代码)
- 在kali linux中你应该知道的信息收集姿势(一)
- 【拓展】谈谈字符编码:Unicode编码与emoji表情编码
- TCP/IP 应用层协议解释
- Cisco Packet Tracer服务器模拟搭建
- Python3调用Google翻译
- 打造最强移动测试平台
- 微信小游戏背后的技术优化
- iox:一款功能强大的端口转发&内网代理工具
- Python3爬取steam优惠
- Cisco实验-将路由器连接到LAN操作步骤
- Vmware安装MacOS