Can someone steal my PHP script without hacking server?

last updated in Categories Apache, Howto, lighttpd, Linux distribution, Networking, php, Security, Tips, Troubleshooting

Adarsh asks:


Can someone steal my PHP code or program without hacking my Linux box? Can someone snoop script over plain HTTP session?

Short answer is no. PHP is server side thingy.

However a misconfigured webserver can easily give out php file to all end users. You need to make sure that mod_php / mod_fastcgi loaded and correct MIME type is setup. To avoid such problem always test your server before moving to production environment. Most Linux distro configures both Apache and PHP out of box.

How do I stop downloading php source code?

The first step should be stopping a webserver.
# /etc/init.d/httpd stop
# /etc/init.d/lighttpd stop

If you are using Lighttpd…

Next bind webserver to for testing purpose. Open lighttpd websever config file and bind server address to
# vi /etc/lighttpd/lighttpd.conf
Bind to localhost/
server.bind = ""
Start lighttpd:
# /etc/init.d/lighttpd start
Now follow these instructions to configure php as fastcgi module. Now test your configuration using url PHP should work on server. If not working, refer to server log file.

If you are using Apache…

Open httpd.conf file and bind apache to
# vi httpd.conf
The Listen directive instructs Apache to listen to more than one IP address or port; by default it responds to requests on all IP interfaces, but only on the port given by the Port directive.
Start apache:
# /etc/init.d/httpd start
Now make sure php is installed use apt-get or rpm command to verify the same:
# rpm -qa | grep -i php
# dpkg --list | grep -i php
If PHP is not installed just follow these instructions to install PHP. Next make sure httpd.conf or php.conf has following directives:
LoadModule php4_module modules/
AddType application/x-httpd-php .php

Note: the path may differ in your setup. Now restart httpd:
# /etc/init.d/httpd restart
A sample php code:

<?php   phpinfo(); ?>

Finally when php started to work properly, make sure you bind back a server IP address from to public IP address.

Another option is keep your source code out of webroot and server all php requests from php application server using mod_proxy and multiple back-end servers.


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

15 comment

  1. One thing to note is the .inc files – many people use this extension when including files in PHP. But many servers give it text/plain(or similar) mime type. This is a huge security risk.

    If your server is not configured properly, people will be able to get this file.

  2. Great advice for the beginner coder!

    Using .inc for extensions isn’t the best way as stated above… Great work 🙂

  3. One tiny little comment I have is that this last paragraph makes it sound as if one would be safe by serving all PHP requests from an application server via mod_proxy.

    Another option is keep your source code out of webroot and server all php requests from php application server using mod_proxy and multiple back-end servers.

    While this does indeed introduce another layer of security to the equation, it also introduces more complexity and therefore more possible points of failure. Furthermore, a misconfigured server behind the proxy will happily hand out PHP code just as freely through a proxy as it would directly, as the proxy pretty much just passes on whatever it gets from the servers it makes it’s requests to…

    That having been said, this should not be an issue at all if people follow your good advice about testing it all on localhost ( and configuring it correctly as you have so helpfully described in such clear detail.

    Also, your advice to keep PHP code out of the webroot is very good advice indeed. Any important PHP code should be kept outside the webroot where the web server cannot accidentally serve it up as plain-text, and should be included using PHP include or require statements so that if something does happen to go wrong, then the worst that could happen is a totally non-essential file might get served up as text, but nothing of any significance would be at risk.

    Thank you very much for posting this useful and informative post, as I am certain it will help many beginners out there. Keep up the good work… 🙂

  4. PHP downloading files can make happen, too. Say you have a file called download.php, with pretty much no security, that downloads a file like this: “download.php?file=file.ext”. somebody could type: “download.php?file=../index.php”, or so.

  5. How about grabbing this php file ehh ?
    any manner?
    please reply

  6. please remove “Tanner January 28, 2011” quotes … this is encouraging hacking … will complain to Google if not removed within 72 hours …..

    1. Listen you fuckquad, Google doesn’t run the Internet and last time I checked these are people’s posts. Don’t like it, go fuck yourself.

    2. You protect yourself from hacking by knowing how its done. Sometimes i go on creepy websites that explain how to scam and hack. Because i want to stay safe and know how it works.

  7. Another factor to consider is backup files. If you edit files on the server some editors are configured to save a backup copy in the same directory with a ~ or # character appended to the filename. These can easily be guessed and will be served as plain text by the web server.

  8. php code is interpreted in the server and in clients it see as plain html, so definitely no way unless someone can get into your server.

    Have a question? Post it on our forum!