How to run a script as another user (without password) on Linux

Probably, it’s one of the most common tasks: running some scripts as another user and not being root. Here’s a solution.

1. Run visudo. If you don’t have it make sure you have sudo installed.

2. Add next line:

myuser ALL=(anotheruser) NOPASSWD: /home/anotheruser/bin/script.py

3. Run:

sudo -u anotheruser /home/anotheruser/bin/script.py

How to put Apache web site into the maintenance mode

This is how you can add put you webisite working under Apache web server into the maintenance mode. Note that you need to have mod_rewrite enabled.

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/maintenance.html
RewriteRule ^.*$ /maintenance.html [L]

If the page maintenance.html exists all requests will be rewritten to it.

How to disable versions expose in PHP and webservers (nginx, Apache)

By default PHP exposes it’s version:

curl -vi localhost
< HTTP/1.1 200 OK
< Server: nginx/1.0.15
< Date: Fri, 01 Feb 2013 23:19:48 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=64
< X-Powered-By: PHP/5.4.8

As you can see just by running GET requst you are able to get nginx and PHP version on the server:

Server: nginx/1.0.15
X-Powered-By: PHP/5.4.8

To disable it modify your /etc/php.ini file:

expose_php = Off

As well as nginx’ http section:

server_tokens off;

And if you use Apache:

ServerTokens Prod