.htaccess redirect all traffic to maintenance page except my IP address

[sam_ad id="49" codes="true"]Hi Guys,   This is a follow up to my previous post. I had to switch one of my website off for general public viewing while I was doing some maintenance work. There are many reasons why you would like to do redirect all website visitors to your maintenance page. Few question that you may ask yourself are Do I want my site visitors to see 404, 403 (depending what you do), while I do my maintenance work Do I wish to update my users about how long still they will need to wait before they can order or get help from my website. Do I want them to feed them useful links from maintenance so that they can view your mirrored website etc etc For any of the above questions if you answered Yes to one of them read below else you can play another game of iBasket on your iPhone :)   Ok So here is the deal Lets first redirect all traffic to your maintenance page Open your .htaccess file and paste the following [crayon-60027754133c7938609402/]   In the above snippet just chage YOUR_MAINTENANCE_PAGE to a fitting name e.g. index_down.html or index_maintenance.html etc Let me try to explain above rule. Above rule says redirect all URLs that does not end with /YOUR_MAINTENANCE_PAGE.html to YOUR_MAINTENANCE_PAGE.html That's Ok if you are just uploading a couple of updates and but what if you are going to change your cart and you need some testing of you new stuff. You guessed it right, we will allow your IP normal access to the website. So change the above rule to this [crayon-60027754133cd396553423/] Make required changes IP address Maintenance file name to above Upload or save your .htaccess file and try to access you website from your home and office. Depending on which IP is allowed will see your website as per normal and other will be redirected to your maintenance page Pre-requites: You will need mod_rewrite module enabled in your apache web … [Read more...]

.htaccess Block all IP but mine to access my website

Hi Guys, Well I am writing this one because I thought I did this stuff today so why not share :) [sam_ad id="54" codes="true"] Well sometimes when you are testing your application online, I mean your website you really don't want general public to get access to your website which is still under construction. Your use case scenario may differ but here is something that you can do really quick and easy using .htaccess file that is really a handy file and generally exists in your website root directory. You can do the same stuff that I am going to show you in actually httpd.conf or apache.conf files too. But lets see what needs to be done in case to achieve the above [crayon-6002775414d5d577276798/]   Simple enough, so we have set a rule for the current website that anyone other than YOUR _IP_ADDRESS should be denied access to your website. Remember to change YOUR_IP_ADDRESS to your own IP address such as 123.234.345.456 In other scenarios if you work from home and office then you will want to add more IP address to be able to access you website. Here is what you can do [crayon-6002775414d62832562955/] Simple so yeah you add more "allow from" lines to your .htaccess file. If you don't already have .htaccess file you will have to create one something like this [crayon-6002775414d63704274758/]   assuming that your website root is /var/www/html In windows you can use your text editor to create one and then upload it to your website root directory. I hope this helps you Cheers … [Read more...]

linux list/find all files owned by a group or user

[sam_ad id="33" codes="true"]Hi Guys I thought I should share this one with you I had to audit my website files on Linux where I needed to see all files in a directory owned by user or group called apache. This is how I did it I used a Linux command (I should say a Language) called find I think this command can find caveman using iPhone :) You can find more about find command here http://linux.about.com/od/commands/l/blcmdl1_find.htm To make your task easier I'll highlight the important part that applies to heading [geshi lang="php" nums="1" highlight="1,3,5" target="_self" ] [crayon-6002775416151021248521/] // find all files owned by a user [crayon-6002775416156166137532/] // find all files owned by a group [crayon-6002775416157436548365/] // find all files owned by an user and group [crayon-6002775416158686425973/] Legend: IN_DIRECTORY :  find command will work on this directory, so replace it by the directory name that you would like to search in -group { group-name} : Find file that belongs to group-name. -user {user-name} : Find file that is owned by user-name. Lets take a look at what I did As you see in the Image above I got file called crontab.bak and did a find with -user and -group. One thing to consider here is that when you use both switches together it makes it read as "find all files owned by -user AND -group". Below Screen grab clarifies it As you see above that I chown'ed crontab.bak file and make user web an owner of this file and when I try to run this command it gives me expected results. As you see that above works fine but the output is not really readable when search returns many files. Now what bob? Ok take a look at screengrab below and see what I did to beautify the output     As you can see that I added a -ls   switch and it beautifies my output. You have an option though to use -ls or -printf to format your output. I hope this … [Read more...]

centos-Red Hat disable selinux or set selinux off

Ok so I had this problem with SELinux so I wanted to turn it off I am not a pro Linux guy thats why I find SELinux very daunting to configure. Instead I just rely on Firewalls whenever I can. Said that this is not an advice. I use SELinux too but in some scenarios So let me show you how you can disable SELinux to off Let me tell you that by disabling SELinux you will be removing a security mechanism on your system set by default when you install your OS.   Open your Shell and use these commands [geshi lang="bash" nums="1" target="_self" ] shell> nano /etc/sysconfig/selinux [crayon-600277541637d061083796/] Below are the content of selinux config file # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of these two values: # targeted - Only targeted network daemons are protected. # strict - Full SELinux protection. SELINUXTYPE=targeted Change SELINUX=enforcing  to SELINUX=disabled  and reboot your system Reboot is required to completely turn off Selinux         … [Read more...]

Periodically check and remove write permission for Others (world writable files) Linux

[sam_ad id="33" codes="true"]Hi Guys So I had this problem where I have to check if someone chown'ed any of files permission to world say o+w which is read as Others can write too. If you are unsure what I am talking about please read how chown works and terminology used by browsing to http://en.wikipedia.org/wiki/Chmod   But for this specific problem I just run a cron job that check every hour if file is world writeable then remove that permission and set it something more secure. I am not going to go in details about the requirements here so here is my crontab entry 00 * * * *  find /path/to/my/directory/* -perm -2 ! -type l  -exec chmod o-w {} \; [crayon-60027754164e7709738807/] Above is read as find all files in /path/to/my/directory/* that are world writable (-perm -2) and the file is not a symbolic link ( ! -type 1). Apply chmod on files that are found with this condition. I hope this helps.     … [Read more...]

How to: Finding particular text in Files or directory Linux

[sam_ad id="33" codes="true"]Well this is relatively simple. Linux comes with variety of utils that can be used to accomplish this task. Before I jump into this topic further I would like you guys have a look at this lovely book on Linux which covers many things. Alright Bob! Lets get started. In this tutorial I am going to use grep command. This command is really really fast to run. I could be wrong here because I am only an intermediate user of Linux and not an Expert :) To find specific text in a file use this command [geshi lang="bash" nums="1" target="_self" ] # grep "text_to_be_found" filename.txt [crayon-600277541666d300693112/] To find text in specific file extensions use this command [geshi lang="bash" nums="1" target="_self" ] # grep "text_to_be_found" *.txt To find in all files under a given directory [geshi lang="bash" nums="1" target="_self" ] # grep "text_to_be_found" /path/to/your/directory/* To find in all files under a given directory and its subdirectories, we introduce -R (recursive switch) [geshi lang="bash" nums="1" target="_self" ] # grep -R "text_to_be_found" /path/to/your/directory/*   … [Read more...]

How to move or copy files of particular extension type from one directory to another directory in Linux?

[sam_ad id="33" codes="true"]Hi Guys, Its really simple to move files from one folder to another but what about some specific files with specific extension. I am sharing the solution that I came up with below. I also ask you to consider this lovely book on your right. This is common when you want to consolidate something under a directory. My use case was that I wanted to move all my .config files from /var/www/html/project folder to /var/www/html/config folder find command was my helper here. To find more about find command type [geshi lang="bash" nums="1" target="_self" ] #man find To achieve this first I created a folder called config under project folder using this command. [geshi lang="bash" nums="1" target="_self" ] [crayon-60027754167f6930845780/] Then I used this command to move all my .config files from /var/www/html/project folder → /var/www/html/config folder [geshi lang="bash" nums="1" target="_self" ] find /var/www/html/project/* -name "*.config" -exec mv -i {} -t /var/www/html/config \; How to read the above find command traverse the /var/www/html/project folder. Important bit is /* here which tells this command to read all files under project folder -name '*.config':  find command will be looking for files whose name matches this pattern i.e. in our case ".config" files -exec mv -i {} -t /var/www/html/config \; -exec perform move command (mv) on each file, replacing {} by the path to the file collected from find command. As an exercise try this [geshi lang="bash" nums="1" target="_self" ] # mkdir a # cd a # touch a.txt # ls -l # cd .. # mkdir b # find /path-to-folder-a/* -name '*.txt' # find /path-to-folder-a/a/* -name '*.txt' -exec mv -i {} /path-to-folder-b/ \; # cd b # ls -l Note replace path-to-folder-a and path-to-folder-b to absolute path of you current working folder which you can get from "pwd" command. Here is a description of above steps Create … [Read more...]

how to kill a process in linux?

So you have a process running that took control of your system resources. hmm.. lets kill that process.. Generally a process loose its way because of loose pointers or a dependent resource is not responding or there can be any number of other reasons. I would generally shutdown the non responding process and start it again so that your system performance do no suffer. Linux comes with commands like kill, killall to kill the processes. I will recommend not using killall for a start. First up is that we would like to know which processes are running in our system, for that we will use ps command as shown below. Read more about this command here http://unixhelp.ed.ac.uk/CGI/man-cgi?ps [crayon-6002775416a05789486479/] [crayon-6002775416a0a709397110/] This will list all the processes running on our system. but say the troubled process is a php script that is stuck in its for loop. I will thus pipe the output of ps command to grep command to find the process ID of this phpscript [crayon-6002775416a0b604993211/] [crayon-6002775416a0c919876805/] The above command will produce an output shown below (example) [crayon-6002775416a0d392700095/] [crayon-6002775416a0e667894663/] We can also utilize a command called pidof as shown below [crayon-6002775416a0f898910178/] The problem with above command is that we can not pin point which php script we need to kill. but for killing processes such as httpd, mysql etc its ok to rely on the output of the above command. now that we got hold of the process that we need to kill that is 14540 we run the command below to kill it. [crayon-6002775416a10645976994/] Read more about kill command here http://unixhelp.ed.ac.uk/CGI/man-cgi?kill The command kill sends the specified signal to the specified process or process group. If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal. For other processes, it may be necessary … [Read more...]

finding an IP in apache access logs and number of times it is repeated

So I got this problem today so while I wrote a single like command just to achieve that I would like to share as well. Lets discuss the problem first: I have to find an I.P. address from the access logs and check the number of times it gets repeated I assume that you are already in the directory where you access logs are. This technique applies to any file type as long as its readable. I started with this command grep "192.168.1.1" 2011-11-18.log  the above command gives me all lines in my access log where this I.P. address is present. now I will just need to count the number of lines returned which is really simple as shown below grep "192.168.1.1" 2011-11-18.log | wc -l I've created a pipe and fed the result of grep command to wc command (wc -l will count lines) you can also write a small script where you can feed all the access log files one by one to the above command and recording the results to analyze afterwards. My access log file names are dates so for me it is easier say for example to find this I.P. by date and say if this I.P. ever visited on a certain date. You can however do this sort thing in several ways anyway. I hope this helps.   … [Read more...]

Linux – How to delete specific files in directory that are n days old

Ok so this happened recently when there was a requirement to delete all files in a Linux directory that were 30 days old. The best I could do is to check the last modified date for each file in the folder in question and then go on deleting them individually but that would have been a nightmare Below is how I did it. There could be a better way out there, as I am not a Linux expert this is the best solution I can come up with. find /home/jaspreet/somefolder/ -mtime +30 -exec rm {} \;  The above should be read as "find all those files in /home/jaspreet/somefolder whose last modified time is more that 30 days and then execute rm on all of them" Please note that we are using absolute path to the folder to find files in it.  Now this works very well for me. You can extend this to say finding all the TXT files in that folder and then executing the rm command on that batch found. I strongly recommend that before you do anything its a wise thing to just run find command and check the returned files are what you really would like to remove. I hope this helps Cheers … [Read more...]