System-Wide Apache Handler and MIME Types

Jim_WHM

Member
Jun 12, 2014
16
0
1
cPanel Access Level
Root Administrator
I have a site that i recoded into php from aspx but I kept the file extensions .aspx so I wouldn't have to 301 redirect everything.
On the cPanel level, for an individual account I know how to add and apache handler and MIME type to make the .aspx file run as .php. But what this actually does is creates a .htaccess file in the /public_html folder with the following lines:
AddType application/x-httpd-php5 .aspx
AddHandler application/x-httpd-php5 .aspx


What I'd really like to do is avoid .htaccess files and put this in the httpd.conf file. I believe this would be faster and there's no possibility of the .htaccess file might get replaced/deleted thereby exposing php code as pure text on the server. But I know that httpd.conf gets messed up when EasyApache is ran. Is there a proper way to do this so using EasyApache/WHM/cPanel wont mess things up?
 

vanessa

Well-Known Member
PartnerNOC
Sep 26, 2006
833
28
178
Virginia Beach, VA
cPanel Access Level
DataCenter Provider
Try editing httpd.conf and finding this block:

Code:
<IfModule mime_module>
    TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

</IfModule>
Add it within that block and run:

Code:
/usr/local/cpanel/bin/apache_conf_distiller --update

I didn't actually try this, but theoretically it should work by applying the change to the config templates so it is retained during updates.

If that doesn't work, go into /var/cpanel/templates/apache2, copy main.default to main.local, add the config to main.local, and run:

Code:
/scripts/rebuildhttpdconf
service httpd restart
 

Jim_WHM

Member
Jun 12, 2014
16
0
1
cPanel Access Level
Root Administrator
Thanks a ton. It works. All i had to do was:
- insert 'AddType application/x-httpd-php5 .aspx' into the block you showed
- ran '/usr/local/cpanel/bin/apache_conf_distiller --update'
- restarted Apache

I guess I don't need to set an AddHandler anywhere
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,218
463
Hello :)

Your changes are preserved if you ran the Apache distiller.

Thank you.
 

Jim_WHM

Member
Jun 12, 2014
16
0
1
cPanel Access Level
Root Administrator
Actually I just rebuilt apache using EasyApache and my .aspx pages were showing as pure text code again. So I guess
/usr/local/cpanel/bin/apache_conf_distiller --update
didn't work right.

I tried your 2nd solution but wasn't 100% sure where to insert the lines. A lot of places in the main.local file say not to edit it.
 

vanessa

Well-Known Member
PartnerNOC
Sep 26, 2006
833
28
178
Virginia Beach, VA
cPanel Access Level
DataCenter Provider
Actually I just rebuilt apache using EasyApache and my .aspx pages were showing as pure text code again. So I guess
/usr/local/cpanel/bin/apache_conf_distiller --update
didn't work right.

I tried your 2nd solution but wasn't 100% sure where to insert the lines. A lot of places in the main.local file say not to edit it.
Same section. Look for this:

Code:
<IfModule [% main.mainifmodulemimemodule.item.ifmodule %]>
    TypesConfig [% main.ifmodulemimemodule.typesconfig.item.typesconfig %]
[% FOREACH dir IN main.ifmodulemimemodule.addtype.items -%]
    AddType [% dir.mime %] [% dir.extension %]
[% END -%]
Add the additional settings after END, but before the closing </ifmodule>. That *might* work.
 

Jim_WHM

Member
Jun 12, 2014
16
0
1
cPanel Access Level
Root Administrator
Thanks I did the edit and ran
/scripts/rebuildhttpdconf
service httpd restart

And I will rerun EasyApache some time tomorrow just to see if it survives.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,218
463
Thanks I did the edit and ran
/scripts/rebuildhttpdconf
service httpd restart
Could you outline the specific steps you took so we can verify if it's the correct method?

Thank you.
 

Jim_WHM

Member
Jun 12, 2014
16
0
1
cPanel Access Level
Root Administrator
Could you outline the specific steps you took so we can verify if it's the correct method?
Thank you.
Sure I just did it again to make sure everything i write is exact:

- The httpd.conf file has not been edited by me, it's fresh off an EasyApache build.
- Logged into SSH with root
- cd to /var/cpanel/templates/apache2
- cp main.default main.local
- vim main.local
- did vim search for 'main.mainifmodulemimemodule.item.ifmodule'
- found it. and edited the block to look like this:

PHP:
<IfModule [% main.mainifmodulemimemodule.item.ifmodule %]>
    TypesConfig [% main.ifmodulemimemodule.typesconfig.item.typesconfig %]
[% FOREACH dir IN main.ifmodulemimemodule.addtype.items -%]
    AddType [% dir.mime %] [% dir.extension %]
[% END -%]
    AddType application/x-httpd-php5 .aspx

</IfModule>
- saved and quit vim
- ran '/scripts/rebuildhttpdconf' by just typing '/scripts/rebuildhttpdconf' in the commandline. It said '/usr/local/apache/conf/httpd.conf OK'.
I thought my httpd.conf file would be at '/etc/httpd/conf/httpd.conf' ? Nevermind one of these paths must be a symbolic link to the
same spot.

- restarted apache using 'service httpd restart'
- Saw no changes in '/etc/httpd/conf/httpd.conf' or '/usr/local/apache/conf/httpd.conf' yet. As in 'AddType application/x-httpd-php5 .aspx' was not added to the httpd.conf file at this point. Same file probably I know (symbolic link)..

- Then reran EasyApache from WHM (Previously Saved config profile) and rebuilt it w/out changing any options.
- saw that 'AddType application/x-httpd-php5 .aspx' was still not added to httpd.conf and my site's .aspx files showed as pure text.
 

Jim_WHM

Member
Jun 12, 2014
16
0
1
cPanel Access Level
Root Administrator
Ok I found that my problem was that I should have been changing the files in this directory instead:
/var/cpanel/templates/apache2_4
not /var/cpanel/templates/apache2

I haven't rebuilt apache yet but I think it will probably work because /scripts/rebuildhttpdconf inserted it correctly into httpd.conf.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,904
2,218
463
I am happy to see you were able to resolve the issue. Thank you for updating us with the outcome.