Category Archives: Apache

Smarty templates needs to be reload twice to see changes

I was having some issues with Smarty, when i updated a template i have to reload it twice to see my changes.

After some digging i realized that Zend OPcache was causing it.

So i jut disable Zend OPcache and now is working as it should.

just edit php.ini as root.

sudo nano /etc/php5/apache2/php.ini

Find the line where “opcache.enable=1” is and change it to 0, if it has a “;” remove it and that’s all

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=0

Then we simple reload apache

  1.  
  2. sudo /etc/init.d/apache2 restart
  3.  

How to disable directory listing in apache?

First option is to edit the file /etc/apache2/sites-avalible/default (i am using ubuntu), then look for the following lines:

  1.  
  2. <Directory /var/www/>
  3.     Options Indexes FollowSymLinks MultiViews
  4.     AllowOverride None
  5.     Order allow,deny
  6.     allow from all
  7.     # This directive allows us to have apache2’s default start page
  8.     # in /apache2-default/, but still have / go to the right place
  9.     #RedirectMatch ^/$ /apache2-default/
  10. </Directory>
  11.  

And then take out the word Indexes, then your file will look like

  1.  
  2. <Directory /var/www/>
  3.     Options FollowSymLinks MultiViews
  4.     AllowOverride None
  5.     Order allow,deny
  6.     allow from all
  7.     # This directive allows us to have apache2’s default start page
  8.     # in /apache2-default/, but still have / go to the right place
  9.     #RedirectMatch ^/$ /apache2-default/
  10. </Directory>
  11.  

Finally we just reload apache

  1.  
  2. sudo /etc/init.d/apache2 restart
  3.  

If you want to use .htaccess, you have two options:

  1.  
  2. # first one is to deny access to indexes
  3. Options -Indexes
  4.  

This option will show a forbidden message:

Forbidden

  1.  
  2. # the second option is to show the directory as empty
  3. IndexIgnore *
  4.  

empty

Block spammers by IP

If your website is being attacked by spammers, you may want to block those users from your website. first thing we will do is to see how many connection by IP we have in our server, we will do this using netstat.

  1.  
  2. netstat -ntu | awk ‘ $5 ~ /^[0-9]/ {print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n
  3.  

Once we have the IP address to block, we will use .htaccess to block those ip address like:

  1.  
  2. order allow,deny
  3. deny from 127.0.0.1
  4. deny from 127.0.0.2
  5. deny from 127.0.0.3
  6. allow from all
  7.  

Protect web pages with username/password

If there some web pages you want to protect with username/password, all you have to do is to create a file called .htaccess under the directory to protect with the following lines:

  1.  
  2. AuthUserFile /path/to/htpasswd/.htpasswd
  3. AuthGroupFile /dev/null
  4. AuthName "Restricted Directory"
  5. AuthType Basic
  6.  
  7. <Limit GET POST>
  8. require valid-user
  9. </Limit>
  10.  

Replace /path/to/htpasswd/ with the .htpasswd path.
Replace Restricted Directory with the text you want as title of the prompt window.

Then you have to create the .htpasswd file using the following command:

htpasswd -c .htpasswd user_name

Execute it under the directory to protect or add the full path to .httpasswd and replace user_name by the username you want to use, you will be asked to enter the password and the file will be created.

Notes: This instruction are for linux/unix systems using apache web server, the dot means hidden files on linux.

Configure Apache to serve PERL pages ( Ubuntu )

I just reinstalled my ubuntu distribution, and have to configure apache to serve perl pages outside of the cgi-bin directory, after a little bit of googling i found the solution.

You have to edit the /etc/apache2/apache2.conf and add to it the following lines.

  1.  
  2. AddHandler cgi-script .cgi .pl
  3. <Files ~ "\.pl$">
  4. Options +ExecCGI
  5. </Files>
  6. <Files ~ "\.cgi$">
  7. Options +ExecCGI
  8. </Files>
  9.  

Stop apache showing incomplete urls

Apache was showing files even if i didn’t put the extension on the url, that behavior was messing with a mod rewrite rule i was working on. To solve this we just add the line below to the .htaccess file and that’s it apache will stop showing incomplete urls

  1. Options -MultiViews
  2.  

Force download in apache

if you want to force certain kind of files to pop up the save as dialog box, you have to add the AddType directive with the value application/octet-stream to the .htaccess file like this:

  1.  
  2. AddType application/octet-stream .csv
  3. AddType application/octet-stream .xls
  4. AddType application/octet-stream .doc
  5. AddType application/octet-stream .avi
  6. AddType application/octet-stream .mpg
  7. AddType application/octet-stream .mov
  8. AddType application/octet-stream .pdf
  9.  

and that’s all, now everything works fine

Configure ubuntu to use mod_rewrite

To enable mod_rewrite on Ubuntu is pretty easy what we have to do is, first we enable the apache module for mod rewrite with this line.

  1.  
  2. sudo a2enmod rewrite
  3.  

After that we have to edit the file /etc/apache2/sites-avalible/default, then we look for this:

  1.  
  2. <Directory /var/www/>
  3.     Options Indexes FollowSymLinks MultiViews
  4.     AllowOverride None
  5.     Order allow,deny
  6.     allow from all
  7.     # This directive allows us to have apache2’s default start page
  8.     # in /apache2-default/, but still have / go to the right place
  9.     #RedirectMatch ^/$ /apache2-default/
  10. </Directory>
  11.  

And then we replace “AllowOverride None” by “AllowOverride All”, this line tells apache to read the .htaccess file, that is the file where we will put our rewrite rules.

I have also taked out the MultiViews option since it was messing with my rewrite rules

  1.  
  2. <Directory /var/www/>
  3.     Options Indexes FollowSymLinks
  4.     AllowOverride All
  5.     Order allow,deny
  6.     allow from all
  7.     # This directive allows us to have apache2’s default start page
  8.     # in /apache2-default/, but still have / go to the right place
  9.     #RedirectMatch ^/$ /apache2-default/
  10. </Directory>
  11.  

finally we just reload apache

  1.  
  2. sudo /etc/init.d/apache2 restart
  3.  

now we have pretty urls for our sites. :)