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
# 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
/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
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
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
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /etc/apache2/serverok-htpaswd
Require valid-user
Now rebuild httpd.conf with
/usr/local/cpanel/scripts/rebuildhttpdconf
Restart Apache with
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
htpasswd -b -c /etc/apache2/serverok-htpaswd USERNAME_HERE PASSWORD_HERE
See Cpanel Server, Apache