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 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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏liulun

CEF C++环境搭建

第一步:下载CEF 到这里下载最新版本的CEF http://cefbuilds.com/ 下载解压之后,大概会看到如下图所示的文件 cefclien...

2497
来自专栏猿天地

Spring Cloud Gateway 限流操作

API网关作为所有请求的入口,请求量大,我们可以通过对并发访问的请求进行限速来保护系统的可用性。

1503
来自专栏Seebug漏洞平台

花式窃取NetNTLM哈希的方法

原文:https://osandamalith.com/2017/03/24/places-of-interest-in-stealing-netntlm-ha...

3008
来自专栏hbbliyong

解决Electron加载带jquery的项目报错问题

<!-- Insert this line above script imports --> <script>if (typeof module === 'o...

3637
来自专栏向治洪

android PakageManagerService启动流程分析

PakageManagerService的启动流程图 ? 1.PakageManagerService概述 PakageManagerService是andro...

44010
来自专栏逸鹏说道

★Kali信息收集★8.Nmap :端口扫描

突然发现,微信一次最多推送8篇 参数:(Zenmap是Nmap图形化工具,不想打指令的可以直接使用) 详细:https://nmap.org/man/zh/in...

4664
来自专栏CDN及云技术分享

Openssl状态机的实现

Openssl是通过“握手“建立加密信道,在该信道双方的身份都是合法的,并且传输数据都是密文传输。Openssl握手通过客户端和服务端互相交换信息计算出secr...

2873
来自专栏xingoo, 一个梦想做发明家的程序员

网络嗅探器

网络嗅探器:把网卡设置成混杂模式,并可实现对网络上传输的数据包的捕获与分析。 原理:   通常的套接字程序只能响应与自己MAC地址相匹配的 或者是 广播形式发出...

36110
来自专栏乐沙弥的世界

Oracle Net Services - Tracing and Logging at a Glance

    Oracle Net trace 用于跟踪或调试oracle连接故障,连接异常断开或者连接超时等情形,通过产生详细的跟踪信息来进行分析和诊断Oracle...

603
来自专栏FreeBuf

Scrounger:一款功能强大的移动端应用程序安全测试套件

今天给大家介绍的是一款名叫Scrounger 的工具,广大研究人员可以使用这款工具来对移动端应用程序的安全性进行测试。首先,这款工具参考和借鉴了很多目前安全社区...

931

扫码关注云+社区