Setup Apache virtual hosts on centos 7
Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run multiple sites off of one server.
step 1:First we need to login with root user and install Apache.
[root@lampblogs ~]# yum install httpd
once it is installed start service and check version.
[root@lampblogs ~]# systemctl start httpd [root@lampblogs ~]# systemctl enable httpd [root@lampblogs ~]# httpd -v Server version: Apache/2.4.6 (CentOS) Server built: Apr 24 2019 13:45:48
step 2: Apache configuration settings
global Apache configuration file in CentOS 7 is /etc/httpd/conf/httpd.conf
The config file has commented lines before every setting that explain their use. So for example, the ServerRoot setting is the top of the directory tree under which lie the Apache configuration files.
The Listen directive binds Apache to a specific IP address and port. Apache’s default listening port is 80.
The user/group values are names of the user/group that HTTPD (apache) runs as. In CentOS the user and group are apache/apache and in Ubuntu/Debian the values are www-data/www-data.
Usually, in most distros the default document root for Apache is set to /var/www/html/ so if you put data in /var/www/html/ you will be able to access that same data via a web browser using your server IP address.
Here we will see how to create sites-available and sites-enabled directories and edit Apache httpd.conf file to apply the new enabled websites
[root@lampblogs ~]# cd /etc/httpd/ [root@lampblogs httpd]# mkdir sites-available sites-enabled [root@lampblogs httpd]# vi conf/httpd.conf
In httpd.conf file add following directive line at the end of the file and save it.
let’s create example.conf file where we will configure a virtual host directive for your domain.
[root@lampblogs ~]# vi /etc/httpd/sites-available/example.conf
Put the following content into this file.
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/var/www/html/example.com/" ServerName example.com ServerAlias www.example.com ErrorLog "/var/log/httpd/example.com-error_log" CustomLog "/var/log/httpd/example.com-access_log" combined <Directory "/var/www/html/example.com/"> DirectoryIndex index.html index.php Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
If you changed DocumentRoot location on your virtual host from default /var/www/html to other path make sure you also create this path.
[root@lampblogs ~]# mkdir -p /var/www/html/example.com
In CentOS 7 Apache runs under apache as user and apache as group. So you will need to recursively assign that values to the files and directories in the document root.
[root@lampblogs ~]# chown apache:apache -R /var/www/html/example.com/
you should create index.html file in example.com and write like test page
[root@lampblogs ~]# cat /var/www/html/example.com/index.html this is test page
Once your configuration file is ready, you need to enable it. Create sympolic link to your configuration file in sites-enabled folder.
ln -s /etc/httpd/sites-available/example.conf /etc/httpd/sites-enabled/example.conf
In order for these changes to take effect, restart the Apache.
[root@lampblogs ~]# systemctl restart httpd
If you are using selinux please disable it.
sed -i 's/enforcing/disabled/' /etc/sysconfig/selinux
if you want to access website in your browser you can add to /etc/hosts file in your system like below.
[root@lampblogs ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.15 example.com
If your domain(website)is public domain you must add 'A' Record which resolves into your web server's IP in DNS control panel.