How to enable mod_rewrite on Apache server - apache

I need to enable mod_rewrite on a Apache server version 2.4.16. The server is running CENTOS 6.7 x86_64 - I tried many different things but nothing worked.
Also, is there anyway I can find out if mod_rewrite is already enabled?
Thanks,
Richard.

A good way to see if it's enabled is by trying to use it.
The apache documentation shows a pretty simple way to test out mod_rewrite . Place the following in an htaccess file:
Redirect "/foo.html" "/bar.html"
And then check if yoursite.com/foo.html redirects properly.
https://httpd.apache.org/docs/2.4/rewrite/remapping.html
a2ensite is not going to work, that's a binary built into the apache package for debian, you mentioned a centos installation.
To determine specifically if mod_rewrite is built into your apache install, use
httpd -M
To list loaded modules and grep for the one you want (rewrite).
[root#server ~]# /usr/sbin/httpd -M | grep rewrite
rewrite_module (shared)
[root#server ~]#

Test 1:
Easiest way to test if mod_rewrite is ON/OFF is to create a php info file
Under “apache2handler” check for “Loaded Modules” section. There will be a list of loaded modules. Check if mod_rewrite is present in the list.
Test 2:
By creating a file called .htaccess and then by typing the following lines in it
Options +FollowSymLinks
RewriteEngine On
How to find if mod_rewrite is enabled in Apache server?

Try with the command:
sudo a2enmod mod_rewrite

Related

Apache configuration: how to get quick feedback?

When writing configuration files for Apache web server I would like to have a quick feedback loop.
I, for example have a script that doesn't seem to work. It is either not picked up, or the variables I use are not set, or maybe overriding is not allowed. How to debug this?
I expected to at least print some debug log statements like REQUEST_URI: %{REQUEST_URI}. Can't find such a thing.
apachectl is a front end to the Apache HyperText Transfer Protocol (HTTP) server. It is designed to help the administrator control the functioning of the Apache httpd daemon.
Here is a link to the documentation.
Different platform might use different binary names such as apache, apache2 or apache2ctl. To test the configuration - just run:
apachectl configtest
# or, depending on your OS
httpd -t
EDIT
If you are trying to debug your virtual host configuration, you may find the Apache -S command line switch useful. That is, type the following command:
httpd -S
This command will dump out a description of how Apache parsed the configuration file

Apache plugin not working for Let's Encrypt

I'm trying to secure my CentOS 7 VPS with Let's Encrypt. I've followed the guidelines in https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-centos-7 . I've set up virtual hosts, installed server dependencies and the Let's Encrypt client. But when I try to set up the SSL certificate with:
./letsencrypt-auto --apache -d example.com -d mail.example.com
I get the error:
The apache plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError()
The Apache Plugin for Let's Encrypt is not working, but how do I make it work?
Same error on CentOS 7 and Apache 2.4. Checked through my configuration, had a couple of IfModule lines that were not closed with /IfModule. Apache is OK with them, but apparently certbot parser isn't. Hope this helps others.
appretnly they are a bug in the letsencrypt-auto script, you can use this command to do the job.
sudo certbot --authenticator standalone --installer apache -d <yourdomain> --pre-hook "systemctl stop apache2" --post-hook "systemctl start apache2"
This answer is not working. In my cases i checked apache config file and found the following line at the end of it <IfModule mod_ssl.c> It was not making sense. On removing it the renewal went on without a problem

How to change the apache user for a virtual host in Centos 7?

I am trying to set up a web server on Centos 7. I have created a virtual host by editing /etc/httpd/conf/httpd.conf. The root directory of the virtual host was set to /var/www/html/domain.com. I am able to open domain.com in the browser with no issue.
However, when I use get_current_user() in a PHP file to test what user the server is running under, the output is "root" which is a security concernt.
How do I change the user of the virtual host? When I set up a similar scenario in Ubuntu, I used apache2-mpm-itk module and configured the virtual host with "AssignUserID your_username your_username" and it did the trick. But I don't know how to do it in Centos.
Thanks
You can try to use SuexecUserGroup for that.
I found this method from plesk's configuration.
<VirtualHost *:80>
<IfModule mod_suexec.c>
SuexecUserGroup "anyuser" "anygroup"
</IfModule>
/VirtualHost>
In CentOS, this command gives you the file's owner, not the owner of the process ID. Something like that. It's in the PHP manual. Check out the comments at the bottom. get_current_user() By the way, check your User and Group directives in the httpd.conf and in httpd-vhost.conf. Also, use ps -faux to get a clear idea of the process spawning relationships between the httpd binary and the children it spawns. Just trying to help. ;-)
Also, check out How Apache Starts at the top of this page to make sure you understand how Apache is supposed to work by default. If yours in not working this way, that may be a good thing to know!! ;-)
In CentOS 7 you can use the httpd-itk package. It is in the EPEL repository, so you need to add that first. Something like this should get it:
yum install epel-release
yum install httpd-itk
Once installed you need to load the module by uncommenting the LoadModule line in:
/etc/httpd/conf.modules.d/00-mpm-itk.conf
Restart the httpd, and then you can use the new AssignUserID directive inside your VirtualHost sections.
have you looked into which user/group is specified in /etc/httpd/conf/httpd.conf? It would look something like this:
User apache
Group apache

Apache mod_rewrite Unavailable

I have problems with apache mod_rewrite. In my vps (var/www/script/) directory script folder, I have a cms and want to install it. Unfortunately it says Apache mod_rewrite Unavailable. I have tried several solutions to resolve this problem:
Tried enable mod_rewrite module:
updatedb
locate mod_rewrite.so
(got /usr/lib/apache2/modules)
cd /etc/apache2/mods-enabled
touch rewrite.load
used editor to paste this line LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.s
edited /etc/apache2/sites-available/default: I have changed AllowOverride None to AllowOverride all
/etc/init.d/apache2 restart
This didn't help
Tried type a2enmod rewrite. Got output :
Module rewrite already enabled
Restarted couple times apache service
Im using debian.
My apache2.conf: http://pastebin.com/9m8H4SKC
Question: What I'm doing wrong and what should I do in order to resolve this problem?
Remove your custom rewrite.load file, run "a2enmod rewrite" again and restart Apache. Anyway, mod_rewrite is generally not needed for CMS, it's just nice to have for SEO and such.

Clean URL's not working, mod_rewrite module installed

I just installed a fresh copy of Drupal 6.19 to get to speed on how to write modules. But for some reason the rewrite module isn't working for Drupal.
What I have checked:
$ apachectl -M >> it is installed
php_info() on current server >> says rewrite is installed also
I also double-checked the .htaccess file in my Drupal root folder
UPDATE: checked httpd.conf for AllowOverride All
I'm out options here. Looked everywhere but the Drupal settings aren't letting me to enable the settings and their test is simply visiting a site that should work if the rewrite module was there.
My specs:
Mac OS X 10.6 Snow Leopard
Using built-in Apache with PHP5
Thanks!
Last but not least, you must authorize your virtual host or directory to use .htaccess.
AllowOverride All
Update:
I can't figure out your exact problem but it's always worth testing that Apache is actually parsing your .htaccess file. Make a syntax error on purpose and see if you get a 500 Internal Server Error message. Also, test mod_rewrite with a simple redirection rule that doesn't involve Drupal.
Two more things to check:
Verify that your .htaccess is readable for your apache processes (Yes, I read that you double checked it, but did that include the file permissions?)
Enable rewrite logging - this might give you some hints on where it fails. Start with a log level of 5 and increase/decrease as needed. (Don't forget to turn it off again later, as it is a huge performance hog ;)
Also, I'd try to simplify the test scenario - start with a simple rewrite directive in a vhost configuration. Once that works, move it to a .htaccess in the top-level of the vhosts document root, then to sub directories (if needed/used).
Make sure that if the install is in a sub-directory that the username is part of the rewriteBase
# Modify the RewriteBase if you are using Drupal in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/drupal uncomment and
# modify the following line:
RewriteBase /~username/drupal**
If you running your Drupal installation in a sub-folder like: example.com/drupal, then enable "RewriteBase /" in your .htaccess file, it might help you.
A clean url could be something like www.example.com/fisherman instead of www.example.com/data/pages/fisherman.php
Some installations of apache have clean URL functionality out of the box. There are 2 steps that need to be configured correctly for it to work.
Enable rewrite module
Allow .htaccess file overrides
Both steps require SSH root access to your server. So if you are on shared host this probably won't work for you. Open your terminal:
Enable rewrite module
To enable the rewrite module, you can type the following command
sudo a2enmod rewrite
Now type
sudo service apache2 restart
to enable the changes. You can check if it’s working by typing
sudo apache2ctl –M
A list will appear. Look for “rewrite_module”.
http://codeontrack.com/enable-clean-urls-apache/
Look for this Line in your httpd.conf file
#LoadModule rewrite_module modules/mod_rewrite.so
If commented
Just uncomment it restart apache server then try enabling in drupal administer Clean URL Section
I added the following to my .htaccess file and it was solved. My problem was specifically hapening with Rackspace / CentOS image
Options -MultiViews
As Álvaro G. Vicario mentioned, the first thing to do is at the top of the .htaccess file add something like ghfdiddfdjf which should throw an internal error. If it doesn't, you know the .htaccess file isn't being read. Mine wasn't, and I found the following fix:
In the etc/apache2/sites-enabled folder, there was a file called default-000. It was in this file that I changed AllowOverride All in 2 places in the file.