About

Thursday, January 30, 2014

How to enable mod_rewrite in Apache2 on Debian or Ubuntu


How to enable mod_rewrite in Apache2 on Debian or Ubuntu


If you have installed Apache2 web server via apt-get or aptitude on Debian or Ubuntu systems, it has mod_rewrite module installed, but not enabled by default. After Apache2 installation, you need to enable mod_rewrite explicitly in order to enjoy its benefit.

What is mod_rewrite?

Apache2 web server boasts of extensible features which are realized by the notion of pluggable modules. When building Apache2, you compile a set of modules you think are necessary, into it. One such module is called mod_rewrite which is responsible for rewriting website URLs at the server side. For example, when user asks for "http://myserver.com/my_category/my_post.html", the requested URL is translated by mod_rewrite to "http://myserver.com/post.php?category=100&post=200", which is then handled by the web server.

Why use mod_rewrite?

Webmasters generally use mod_rewrite to improve user-friendliness and search engine friendliness of web sites by exposing more memorable and crawlable URLs to the world with mod_rewrite. Also, it can help hide any sensitive information such as query strings from URL requests, and hence can enhance website safety.

How to enable mod_write on Apache2

The default installation of Apache2 comes with mod_rewrite installed. To check whether this is the case, verify the existence of /etc/apache2/mods-available/rewrite.load.

$ cat /etc/apache2/mods-available/rewrite.load
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

To enable and load mod_rewrite, do the rest of steps.
$ sudo a2enmod rewrite

The above command will create a symbolic link in /etc/apache2/mods-enabled.
$ ls -al /etc/apache2/mods-enabled/rewrite.load
lrwxrwxrwx 1 root root 30 Dec  9 23:10   /etc/apache2/mods-enabled/rewrite.load -> ../mods-available/rewrite.load

Then open up the following file, and replace every occurrence of "AllowOverride None" with "AllowOverride all".

$ sudo vi /etc/apache2/sites-available/default
Finally, restart Apache2.
$ sudo service apache2 restart

setup-nginx-for-codeigniter-on-ubuntu

I recently launched my site - Curriculum Vitae - which is built , mostly, with PHP and MySQL, the regular LAMP stack. And of course I used the great Twitter Bootstrap and Codeigniter Frameworks.
The site is running a on a VPS with Ubuntu 11.10. It all was very easy to setup as at first I used Apache2 which is a great server and the site was running for 2 weeks with this configuration but I kept reading on Nginx and saw a real benefit to having a reverse proxy already setup handling requests.
So I set out create a server that would run my CodeIgniter Site and keep PHP as fast as I could.

Step 1 - Updating Apt with sources

As we are running Ubuntu (But this should work in Debian too, worth a try right?) we would want to use apt. So first let's get a fresh package list.
sudo apt-get update

Step 2 - Installing Nginx

Thanks to apt this is really easy.
sudo apt-get install nginx
That just too easy isn't it no need to compile! Test that you Nginx is indeed working by going to localhost:80, if not use some Google-fu.

Step 3 - Installing PHP5

Again thanks to Apt this is just as easy. This is the usual suite of modules that I install so customize to your own needs.
 
sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
Check if install by successful by issues the command

php -v
If your Version of PHP is shown all is OK if not, check for any errors that may have occurred during install.

Step 4 - Installing PHP-FPM

Nginx is quite diffrent from Apache. First and foremost it's non-blocking while Apache blocks, but for our context Nginx does not run PHP itself. Nginx was built as a reverse proxy, so it simply forwards requests to the specified url / address. So to run PHP we need a container that would run the PHP and act as the host where Nginx is sending requests to. This is where PHP-FPM comes in. PHP runs as a container and runs the PHP files when requests come in. Great so lets install it!
 
apt-get install php5-fpm
That should install FPM and start the service. Great so we are half-way there!

Step 5 - Configure Nginx to forward requests for PHP to PHP-FPM

Now we have a default installation of Nginx and PHP-FPM running. Wow! Let's configure Nginx to forward requests to PHP-FPM and in a way that would allow CodeIgniter to run just as it would have on Apache with Rewrite. I'm assuming your using Rewrite and no Query String Url. Edit Nginx's Site Configuration at /etc/nginx/sites-available/default and include the following configuration:

server {
    server_name example.com;
    root path_to_your_public_root_dir;

    index index.html index.php index.htm;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires max;
        log_not_found off;
    }

    location / {
        # Check if a file exists, or route it to index.php.
        try_files $uri $uri/ /index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}
Replace servername, root, fastcgipass with your enviroment values.
If you plan on hosting multiple CodeIgniter sites with this Instance, think about moving the common settings to a file and including the file.
So the  /etc/nginx/sites-available/default would contain:
server {
    server_name example.com;
    root path_to_your_public_root_dir;
    include /etc/nginx/ci_host;
}
and /etc/nginx/ci_host would contain:
index index.html index.php index.htm;

# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
    expires max;
    log_not_found off;
}

location / {
    # Check if a file exists, or route it to index.php.
    try_files $uri $uri/ /index.php;
}

location ~* \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Step 6 - Restart and Rejoice !

After all the configration is done restart Nginx by using:
sudo service nginx restart
If you visit your configured site you it should show and behave just like to would have under Apache 2 with Rewrites. Good Job!
For a bit of a speed boost consider install PHP-APC too. The combination of Nginx, PHP-FPM and PHP APC is very fast!

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Lady Gaga , Salman Khan