Modify Apache Virtual Host for a site in Cpanel Server
On Cpanel server Apache configuration file get recreated every time you modify or add a new web site. In cause you need to modify VirtualHost entry for a web site hosted in cpanel, you should not edit /etc/apache2/conf/httpd.conf file.
If you open /etc/apache2/conf/httpd.conf and check virtual host entry for a web site, you will see line like
1 2 |
# Include "/etc/apache2/conf.d/userdata/std/2_4/USERNAME/DOMAINNAME/*.conf" # Include "/etc/apache2/conf.d/userdata/ssl/2_4/USERNAME/DOMAINNAME/*.conf" |
in non-ssl and ssl virtual host entry for a domain. To modify virtual host entry, you need to create the folder, then create a file with extension .conf
Then rebuilt httpd.conf by running
1 |
/usr/local/cpanel/scripts/rebuildhttpdconf |
During rebuild, the script will check for the conf file, if found the line get uncommented (remove # from beginning of Include line).
For a web site, i need to protect it with password. This is a feature available in cpanel, but i want it done on server side, so no one accidently make this site public by deleting .htacess.
To do this, i created folders
1 2 |
mkdir -p /etc/apache2/conf.d/userdata/std/2_4/serverok/demo.serverok.in/ mkdir -p /etc/apache2/conf.d/userdata/ssl/2_4/serverok/demo.serverok.in/ |
In this cause, cpanel user name is “serverok” and the web site url is demo.serverok.in
I created .conf file in both folders
1 2 |
vi /etc/apache2/conf.d/userdata/std/2_4/serverok/demo.serverok.in/password.conf vi /etc/apache2/conf.d/userdata/ssl/2_4/serverok/demo.serverok.in/password.conf |
Add content
1 2 3 4 5 6 7 8 9 10 11 |
<Directory "/home/serverok/public_html/"> AuthType Basic AuthName "Restricted Content" AuthUserFile /etc/apache2/serverok-htpaswd Require valid-user </Directory> Now rebuild httpd.conf with <pre> /usr/local/cpanel/scripts/rebuildhttpdconf |
Restart Apache with
1 |
systemctl restart httpd |
Now when i visit the web site, it ask for password. We have not yet created password file, so create it with
1 |
htpasswd -b -c /etc/apache2/serverok-htpaswd USERNAME_HERE PASSWORD_HERE |
See Cpanel Server, Apache