EvilZone
Programming and Scripting => Web Oriented Coding => : 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:
- sudo apt-get install apache2
- sudo apt-get install php5
- sudo apt-get install libapache2-mod-php5
- sudo /etc/init.d/apache2 restart
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)
-
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
;)
-
You could also use
sudo service apache2 restart
instead of
sudo /etc/init.d/apache2 restart
-
You could also use
sudo service apache2 restart
instead 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.
-
service apache2 restart works fine on any distro(almost) if you have sysvconfig installed.
sudo apt-get install sysvconfig
:)
-
Awesome guide Thanks :)
-
Does this still work if your using wamp?
Luffy
-
Does this still work if your using wamp?
Luffy
Yes.
But I recommend LAMPP/XAMPP.
-
I use xampp, it's badass!
http://www.apachefriends.org/en/xampp.html (http://www.apachefriends.org/en/xampp.html)
-
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 :)
-
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?
-
Ok, as i said it was just an idea :P
-
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.
-
Thats "the other side" of my mind :P Have you ever read HEAD FIRST PHP & Mysql?
-
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.
-
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.
-
I would suggest using EasyPHP . It's portable and easy to modify.
-
i was wondering what the picture was showing? Since it have been removed and im trying this execelt guide of PHP programming
Kondiii
-
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.
-
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 :/
-
Kondiii, just download easyphp . If you get lost in the apache instalation you surely won't make trough installing mysql/phpmyadmin :)
-
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.
-
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
-
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?
-
http://windows.php.net/download/
-
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?
-
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.