EvilZone

Programming and Scripting => Web Oriented Coding => : ande October 03, 2010, 04:43:30 AM

: Starting PHP scripting - Setting up a PHP environment
: ande October 03, 2010, 04:43:30 AM
Starting PHP scripting
Setting up a PHP environment

Tutorial by ande for Evilzone

Contents _
0x00 - PHP
0x01 - Software
0x02 - Starting PHP scripting



0x00 - PHP
PHP - A script language used for various tasks, but mostly known for its use in web pages. However, PHP can also be used as command line scripts locally or remotely. In this text I will only be showing how to set up an environment for web page scripting. PHP can be set up on most OS's, I will be covering Windows and Debian(Or any other Linux distribution with APT installed).



0x01 - Software
In order to run any PHP script what so ever you will need the PHP engine and a PHP supporting web server, in this case we will use Apache.

Windows
Apache for windows can be downloaded at www.apache.org (http://www.apache.org), http://httpd.apache.org/download.cgi#apache22 (http://httpd.apache.org/download.cgi#apache22)
Once installed, you should see "It works!" in your browser when you write http://127.0.0.1 (http://127.0.0.1), now move on to PHP.

PHP for windows can be downloaded at www.php.net (http://www.php.net), http://windows.php.net/download/ (http://windows.php.net/download/), you probably want this one: http://windows.php.net/downloads/releases/php-5.3.29-Win32-VC9-x86.msi (http://windows.php.net/downloads/releases/php-5.3.29-Win32-VC9-x86.msi)
When you come to this step of the installer:
(http://foundationphp.com/images/install02.jpg)
Select Apache 2.2.x Module and continue and select your apache config folder when it asks you to(the folder containing httpd.conf, by default its something like C:\Program Files\Apache Software Foundation\Apache2.2\conf\)

Restart Apache and make a file named index.php in your htdocs directory(by default its something like C:\Program Files\Apache Software Foundation\Apache2.2\htdocs) and write:
:
<?php echo ("It works! PHP"); ?>

Now, go to http://127.0.0.1/ (http://127.0.0.1/) and you should see "It works! PHP". If so, you are ready to go!


Linux
To install Apache and PHP run these commands:
Now create a file in the web directory(by default its something like /var/www/) called index.php and write:
:
<?php echo ("It works! PHP"); ?>

Now, go to http://127.0.0.1/ (http://127.0.0.1/) and you should see "It works! PHP". If so, you are ready to go!




0x02 - Starting PHP scripting
Believe it or not but you have already done a little bit of scripting already, by creating the test page saying "It works! PHP"! And this is how you create PHP scripts. Create a .php file, put PHP script tags inside it, and add script. Here is a few more PHP examples.

Simple Hello World print
:
<?php
     
echo ("Hello World");
?>


Variable print
:
<?php
     $Variable1 
"Hello World";
     echo 
$Variable1;
?>


Math + print
:
<?php
     $Var1 
10;
     
$Var2 10;
     
$Var3 $Var1 $Var2;
     echo 
$Var3;
?>


PHP + HTML
:
<html>
<body>
    <?php
          
echo ("Line1 <br /> Line2 <br /> Line 3");
    
?>

</body>
</html>

A great site for further quick learning: http://www.tizag.com/phpT/ (http://www.tizag.com/phpT/)
A great example site for PHP and many other languages: http://www.java2s.com/Code/Php/CatalogPhp.htm (http://www.java2s.com/Code/Php/CatalogPhp.htm)
: Re: Starting PHP scripting - Setting up a PHP environment
: PublicEnemy November 27, 2010, 03:12:01 PM
You can simplify this:
:
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
by
:
sudo apt-get install apache2 php5 libapache2-mod-php5 ;)
: Re: Starting PHP scripting - Setting up a PHP environment
: 10n1z3d November 28, 2010, 11:43:37 PM
You could also use
:
sudo service apache2 restartinstead of
:
sudo /etc/init.d/apache2 restart
: Re: Starting PHP scripting - Setting up a PHP environment
: iTpHo3NiX November 29, 2010, 12:23:54 AM
You could also use
:
sudo service apache2 restartinstead of
:
sudo /etc/init.d/apache2 restart

Depends on the OS, I know that with Debian, service apache2 restart doesn't work. Also I know on CentOS its actually:

:
service httpd restart (sudo implied if not running root)

I can't speak for other distros as I've only really used Debian, Ubuntu, BackTrack, and CentOS, for servers Debian and CentOS really.
: Re: Starting PHP scripting - Setting up a PHP environment
: ande November 29, 2010, 12:00:04 PM
service apache2 restart works fine on any distro(almost) if you have sysvconfig installed.
sudo apt-get install sysvconfig

:)
: Re: Starting PHP scripting - Setting up a PHP environment
: Bursihido March 27, 2011, 01:00:01 AM
Awesome guide Thanks :)
: Re: Starting PHP scripting - Setting up a PHP environment
: luffy12 January 24, 2012, 09:50:45 AM
Does this still work if your using wamp?

Luffy
: Re: Starting PHP scripting - Setting up a PHP environment
: Kulverstukas January 24, 2012, 11:53:12 AM
Does this still work if your using wamp?

Luffy

Yes.
But I recommend LAMPP/XAMPP.
: Re: Starting PHP scripting - Setting up a PHP environment
: dataspy April 03, 2012, 03:19:36 AM
I use xampp, it's badass!

http://www.apachefriends.org/en/xampp.html (http://www.apachefriends.org/en/xampp.html)
: Re: Starting PHP scripting - Setting up a PHP environment
: poscore June 25, 2012, 12:31:12 PM
The sticky is an introduction about PHP programming. Nice :D I was wondering if there any members here who could help, maybe group a team and write a full PHP tutorial?


Its just an idea hoping for responce :)
: Re: Starting PHP scripting - Setting up a PHP environment
: Kulverstukas June 25, 2012, 01:18:01 PM
The sticky is an introduction about PHP programming. Nice :D I was wondering if there any members here who could help, maybe group a team and write a full PHP tutorial?
or maybe you should go ahead and read a book?
: Re: Starting PHP scripting - Setting up a PHP environment
: poscore June 25, 2012, 01:35:14 PM
Ok, as i said it was just an idea :P
: Re: Starting PHP scripting - Setting up a PHP environment
: ande June 25, 2012, 02:45:19 PM
The sticky is an introduction about PHP programming. Nice :D I was wondering if there any members here who could help, maybe group a team and write a full PHP tutorial?


Its just an idea hoping for responce :)

There are already a ton of good tutorials and guides out there. You are better of asking questions here.
: Re: Starting PHP scripting - Setting up a PHP environment
: poscore June 25, 2012, 03:02:57 PM
Thats "the other side" of my mind :P Have you ever read HEAD FIRST PHP & Mysql?
: Re: Starting PHP scripting - Setting up a PHP environment
: ande June 25, 2012, 06:54:35 PM
Thats "the other side" of my mind :P Have you ever read HEAD FIRST PHP & Mysql?

Nope, started PHP after I had done C# and C++ so it kinda came naturarly after some experimenting. As well as the awesome PHP documentation makes reading a book unnecessary.
: Re: Starting PHP scripting - Setting up a PHP environment
: ajmal.josh August 31, 2012, 02:24:37 PM
It is very easy to create php environment with WAMP server in windows OS. WAMP stands for Windows Apache MySQL PHP. Install wamp server and test your php applications. But some problem on wamp server are some antiviruses will not allow wamp server to start functioning(eg. ESET) and also if you installed IIS server you need to stop IIS.
: Re: Starting PHP scripting - Setting up a PHP environment
: Simba August 31, 2012, 04:41:19 PM
I would suggest using EasyPHP . It's portable and easy to modify.
: Re: Starting PHP scripting - Setting up a PHP environment
: Kondiii September 11, 2012, 04:40:31 PM
i was wondering what the picture was showing? Since it have been removed and im trying this execelt guide of PHP programming








Kondiii
: Re: Starting PHP scripting - Setting up a PHP environment
: ande September 11, 2012, 05:56:07 PM
i was wondering what the picture was showing? Since it have been removed and im trying this execelt guide of PHP programming



Kondiii

I am sorry, I dont know how that happend, its been on the EZ server all the time as far as I know :S
I found a new/same pic and updated the original post.
: Re: Starting PHP scripting - Setting up a PHP environment
: Kondiii September 12, 2012, 07:49:46 PM
sorry to bother again, i noticed the 5.2.0 version is outdated and no longer exist, so tried with 5.4 something, but in the installentions i got lost, since it dont show the same options as 5.2.0 :/
: Re: Starting PHP scripting - Setting up a PHP environment
: Simba September 12, 2012, 09:20:27 PM
Kondiii, just download easyphp . If you get lost in the apache instalation you surely won't make trough installing mysql/phpmyadmin :)
: Re: Starting PHP scripting - Setting up a PHP environment
: EmilKXZ September 12, 2012, 11:24:38 PM
And for when you're a little bit more advanced with PHP, you'll perhaps want to go beyond your own system to a "closer-to-production" environment, so if you want to start coding websites as a professional task, adapting scripts or creating your own CMS from scratch, you'll want for sure to get shared hosting for your needs. Anything cheap will do the job (just make sure is not cheap in quality either). When a client asks you to set up a website, you'll want to be already knowledgeable in cPanel/Plesk/Ferozo.

If you want to get closer to more "hand-made" or "purpose-specific" PHP programming, you'll want a VPS. Now if you want to avoid any incurring costs, since it could be to you just a time-killer, you'll probably want to setup a virtual machine. I personally do that with Ubuntu Server, but many others do recommend CentOS, ArchLinux if you want something really lightweight, etc. I bet you'll find your favorite distro this way.
: Re: Starting PHP scripting - Setting up a PHP environment
: Kondiii September 13, 2012, 12:52:02 AM
Kondiii, just download easyphp . If you get lost in the apache instalation you surely won't make trough installing mysql/phpmyadmin :)


only got lost because the new PHP 5.4 only have 3 options compared to 5.2.0 that was the problem :D  but there is only 1 way to learn it and thats keep trying to you get it right :P
: Re: Starting PHP scripting - Setting up a PHP environment
: Ev0lve October 26, 2013, 08:57:28 AM
Hey I have been having a problem where I cannot find the installer and the re-download will not fix it.  Any advice at all?

: Re: Starting PHP scripting - Setting up a PHP environment
: ande October 26, 2013, 02:54:08 PM
http://windows.php.net/download/
: Re: Starting PHP scripting - Setting up a PHP environment
: Resistor February 12, 2014, 07:18:12 AM
Hi, I was wondering if anyone could recommend any good books for studying the LAMP stack and implementing mail servers?

Also, is it better to study each area with a separate book, ie get a book on Apache, a book on MySQL, one on PHP, one on Linux, or is it better to read books that focus on teaching two or more LAMP components simultaneously?
: Re: Starting PHP scripting - Setting up a PHP environment
: Schalla July 02, 2014, 07:39:05 PM
Hello there,

just to add a few notes, which may be useful:

1. xDebug

xDebug allows you to debug PHP code. It is a PHP extension, which can be used within
CLI but also Apache. You can set breakpoints and watch variables, like used from normal
debuggers when programming e.g. C/C++/Java.

You can define how to connect and so on, I may recommend to use ssh port forwarding
if you want to debug on external server, since xDebug builds by default a listener on port 9000
and only accepts local host requests.

You can find tutorials for NetBeans/Eclipse in the net, if you want to get a little bit more serious
I can only recommend to get PHPStorm from JetBrains, which is still my favorite IDE.

2. Server Enviroment

If you are setting up a server, please remember to harden it.

Some fancy things you may want to consider:

2.1 Server Tokens

By default the ServerTokens are set to Full, when I remember correct.

However:
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.

I guess you don't want that.

Go to:

/etc/apache2/conf-avaiable/security.conf

And change it to:

ServerTokens Prod

2.2 ServerSignature

# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).

Nope, thanks.

Same file as above:

From:
ServerSignature On

To:
ServerSignature Off

2.3 PHP Info

There is a "neat" feature in the php.ini:

; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header).  It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; http://php.net/expose-php
 expose_php = Off

Change it to off. It's located in /etc/php5/apache2/php.ini

2.4 Disable Functions

In most cases you don't want to allow exec(); and system(); on your service, disable it using:
; This directive allows you to disable certain functions for security reasons.
; It receives a comma-delimited list of function names. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
; http://php.net/disable-functions
disable_functions =

You can enable it using vHosts also.

2.5 Open Basedir

PLEASE enable Open Basedir per virtual host. This denies access from the webserver to the
system, it's such a simple tweak and still isn't done quite often. This just weakens LFI by quite a bit.

-----------------------------------------------------------------------------------------------------------

Resistor:
That guide was quite useful to set up a nice webserver.
https://www.exratione.com/2014/05/a-mailserver-on-ubuntu-1404-postfix-dovecot-mysql/

I hope I could add something useful here.

Best Regards,
Schalla

//EDIT

The WYSWYG Editor fucked up the syntax somehow.