SATOHOST
No Result
View All Result
Thứ Năm, Tháng Năm 19, 2022
  • Login
  • vps giá rẻ
Subscribe
SATOHOST
  • vps giá rẻ
No Result
View All Result
SATOHOST
No Result
View All Result
Home Blog

How to Configure Apache Virtual Hosts on CentOS 7

admin by admin
Tháng Tám 12, 2019
in Blog
5 min read
0
154
SHARES
1.9k
VIEWS
Share on FacebookShare on Twitter

Today, we will be reviewing how to configure Apache virtual hosts on a CentOS 7 server. If you host websites, chances are you are hosting more than one website. If so, knowing how and why these virtual hosts work should allow you to better understand why they are needed.
By default, Apache can host only one document root for all requests, which likely isn’t what you want to happen.

We can use VirtualHost blocks to translate named domains into their appropriate document roots, with new settings per-block as needed. But, what goes into a valid VirtualHost? Where should it be stored?

Pre Flight Check

For this article, we will be using a Core-Managed CentOS 7 VPS, which comes with a clean installation of Apache 2.4. Instructions will be similar for CentOS 6, and also for Unmanaged CentOS 7 servers, with the same Apache version. We’re SSHed to our server as the root user.

 

Step 1: Set Up Apache

Apache will be configured to host multiple sites out of the gate, so there is not much to do here. Historically (that is, in Apache 2.2), NameVirtualHost was necessary to tell Apache that we wanted to use Virtual Host blocks to host multiple sites. Since this is a prevalent option, it is now on by default in Apache 2.4, so we don’t have any additional configuration to do. But, we do want to make sure that Apache is enabled and will start at boot.

systemctl enable httpd
systemctl start httpd

These two commands should have no output. If they do have output, there was a warning or error, which you can see details of by running:

systemctl status httpd

Apache is now up and running and ready to host sites!

 

Step 2: Add Document Roots

We now need some domains to add to Apache. Let’s set up two new document root folders, one at /var/www/domain.com/ and one at /var/www/domain.net.

mkdir -p /var/www/domain.net
mkdir -p /var/www/domain.com


Now, you can start to add unique content to each folder. Adding a basic index.html file that says what domain you are loading as this will help us check our work later.

mkdir -p /var/www/domain.net
mkdir -p /var/www/domain.com

If you have secured Apache to run under a different user, you should update the ownerships and permissions. Otherwise, Apache will run as root with root file ownership.

 

Step 3: Add Virtual Host Files

Now that we have sites to host, we need to tell Apache about them. We will make a new file for each domain, so that we can enable or disable (or add and remove) down the line. Write the following to a file at /etc/httpd/conf.d/domain.com.conf:

Let’s break these lines down.

The very first line opens our VirtualHost block and defines what IP address and port we will listen on for this directive. The asterisk identifies all IPs on the server, and port 80 as the non-SSL port since we don’t have an SSL yet.

The only two lines you are required to have in a VirtualHost block are ServerName and DocumentRoot. These tell Apache what inbound domain requests to listen for, and what folder to serve those requests.

The ServerAlias can describe other domains that should serve the same document root, which can be handy, in say a WordPress multisite installation.

The ErrorLog line tells Apache where to store errors related to loading this domain. A single unique file can separate logs per domain, such as domain.com.error.log.

CustomLog with the ‘combined’ option will log all access requests for this domain. This log is also especially useful when troubleshooting issues that happen when a request is made. Also, this can be very useful for keeping track of what IP’s are accessing your site.

There are also other helpful lines you could add to your VirtualHost block:

  • ServerAdmin declares the email address of the webmaster and is provided to visitors if an error is encountered. Other options can be set here as well; Like in a .htaccess file, we can turn off index pages or symlinks.
  • RewriteEngine can be enabled in a VirtualHost
  • Also, other conditions and rules for rewrites

Almost anything you can put in a .htaccess file can also go in a VirtualHost configuration file. Now that we have a VirtualHost in place for domain.com, make another one for domain.net the same way.

 

Step 4: Test And Restart Apache

Once the files are written, we can run a config test:

httpd -t


This command will return

Syntax OK

if there are no problems seen. If there are issues, the httpd command should tell us what line, as well as what file caused the concern. Now we can reload the configuration i
nto Apache:

systemctl reload httpd


Again, if the reload is successful, this command will have no output. We can confirm that we loaded the VirtualHosts by running:

httpd -S


The output of this command will show Apache’s current running configuration, including the VirtualHost blocks that are loaded. You should see two entries that say ‘*:80’ at the beginning of the line, which will be domain.com and domain.net. You will also see the configuration file that the VirtualHost was loaded from, in parentheses

If you have set up the DNS entries for the domains, you can now visit the domains in your browser, and you will see the two index.html files we wrote earlier. If you don’t have DNS set up yet, you can test using hosts file modification on your workstation.

Finished! Now that our VirtualHosts are done and running, we can continue configuring our websites, securing our server, or setting up SSLs for the newly installed domains.

Do you have other thoughts or concerns? If so, the Liquidweb support team can help. Simply open a ticket with us today at [email protected], give us a call at 1-800-580-4985 or, drop us a line via a chat to speak to one of our Level 3 Support Admins today!

0
Tags: apacheblogCentOS 7httpdhttpd_confvirtual host
admin

admin

Related Posts

edit post
Blog

LINUX BACKUP & RESTORE FULL OS

Tháng Ba 23, 2022
edit post
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z
Blog

VPS Ram 4GB Chỉ 199k/tháng Miễn Phí Gsuite,

Tháng Ba 23, 2022
edit post
Blog

Tài Khoản Google Drive Unlimited 2020 50k – Google Drive Không Giới Hạn – Google drive unlimited 2020

Tháng Bảy 11, 2020
edit post
Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)
Blog

Bán tài khoản MOVO CASH USA ACCOUNT (VCC+VBA)

Tháng Bảy 11, 2020
edit post
Blog

Hướng dẫn kích hoạt bản quyền Windows Server 2012 R2 không cần crack

Tháng Sáu 10, 2020
edit post
Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z
Blog

Hướng dẫn cài đặt và cấu hình Openstack toàn tập từ A-Z

Tháng Sáu 4, 2020
edit post
Hướng dẫn Tự Tạo Ứng Dụng Họp Online Phòng Học Online Miễn Phí với Jitsi
Blog

Hướng dẫn Tự Tạo Ứng Dụng Họp Online Phòng Học Online Miễn Phí với Jitsi

Tháng Tư 13, 2020
edit post
Hướng dẫn sử dụng Remote Desktop để truy cập VPS
Blog

Hướng dẫn sử dụng Remote Desktop để truy cập VPS

Tháng Tư 12, 2020
edit post
Download Kali Linux 2020.1
Blog

Download Kali Linux 2020.1

Tháng Tư 12, 2020
vps giá rẻ
  • vps giá rẻ
Call us: 0975757375

© 2020

No Result
View All Result
  • vps giá rẻ

© 2020

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
  • Sign in

Forgot your password?

Lost your password? Please enter your email address. You will receive mail with link to set new password.

Back to login