Configurring PHP Web Development Platform



Wamp Server 2.0

WampServer is a Windows web development environment. It allows you to create web applications with Apache, PHP and the MySQL database. It also comes with PHPMyAdmin to easily manage your databases.

WampServer installs automatically (installer), and its usage is very intuitive. You will be able to tune your server without even touching the setting files.

WampServer is the only packaged solution that will allow you to reproduce your production server. Once WampServer is installed, you have the possibility to add as many Apache, MySQL and PHP releases as you want.


Download Wamp 2.0

Installing 'wamp'


Double click on the downloaded file and just follow the instructions. Everything is automatic. The WampServer package is delivered whith the latest releases of Apache, MySQL and PHP.

Once WampServer is installed, you can add other releases by downloading them on this website. They will then appear in the WampServer menu and you will be able to switch releases with a simple click.
Each release of Apache, MySQL and PHP has its own settings and its own files (datas for MySQL).





Building a LAMP Server



Installing LAMP on Debian



  • Apache 2 - Linux Web server
  • MySQL 5 - MySQL Database Server
  • PHP4/5 - PHP Scripting Language
  • phpMyAdmin - Web-based database admin software.

Note: Linux + Apache + MySQL + PHP/Perl together commonly known as LAMP Server.

First, let us prepare a system that has a minimum requirement of Debian/Ubuntu version of linux with atleast 256MB of RAM available. Anything less than this minimum ram will cause lot of problems since we are running a server along especially mysql and webmin requires lot of RAM to run properly. Mysql will give you this nasty error "cannot connect to mysql.sock" if you dont have enough memory in your server.

I love debian/ubuntu based linux because of my enormous affinity towards this command apt-get. As a starter knowing this one command, It is so easy to install packages and you dont need to worry about package dependency and configuration. You need to buy a dedicated server or a VPS package if you want to setup your own server. If you want to experiment with the server and installation it is recommended to buy a vps package from various hosts. I prefer vpslink because of their pricing. Believe it or not it is so easy to install and configure your server yourself eventhough you are new are to linux and dedicated/vps hosting.

First download PuTTy if you are accessing your server through SSH. Just enter the IP of your server with root login to access your host. As you probably know, Webmin is a freely available server control panel and we will setup this once we have completed the LAMP server and Mail Server. Webmin makes more easier for us to fine tune our linux box.

Before proceeding to install, update the necessary packages with debian with this command.

apt-get install update

1. Installing Apache + PHP

Apache is one of the most famous web server which runs on most linux based servers. With just few commands you can configure apache to run with PHP 4 or PHP 5.

If you want to install PHP 4, just apt-get

apt-get install apache2 php4 libapache2-mod-php4

To install PHP5, just run the following on linux shell. Note that if you dont specify packages with '4', PHP5 will be automatically installed.

apt-get install apache2 php5 libapache2-mod-php5

Apache configuration file is located at: /etc/apache2/apache2.conf and your web folder is /var/www.

To check whether php is installed and running properly, just create a test.php in your /var/www folder with phpinfo() function exactly as shown below.

nano /var/www/test.php

# test.php

Point your browser to http://ip.address/test.php or http://domain/test.php and this should show all your php configuration and default settings.

You can edit necessary values or setup virtual domains using apache configuration file.

2. Installing MySQL Database Server

Installing mysql database server is always necessary if you are running a database driven ecommerce site. Remember running mysql server to a fair extend requires atleast 256mb of RAM in your server. So unless you are running database driven sites you dont absolutely need mysql. The following commands will install mysql 5 server and mysql 5 client.


apt-get install mysql-server mysql-client php5-mysql

Note: If you have already installed php4, you should make a slight change like this.

apt-get install mysql-server mysql-client php4-mysql

The configuration file of mysql is located at: /etc/mysql/my.cnf

Creating users to use MySQL and Changing Root Password

By default mysql creates user as root and runs with no passport. You might need to change the root password.

To change Root Password

mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD('new-password') WHERE user='root';
mysql> FLUSH PRIVILEGES;

You must never use root password, so you might need to create a user to connect to mysql database for a PHP script. Alternatively you can add users to mysql database by using a control panel like webmin or phpMyAdmin to easily create or assign database permission to users. We will install Webmin and phpmyadmin during later once we complete basic installation.

3. PhpMyAdmin Installation

PhpMyAdmin is a nice web based database management and administration software and easy to install and configure under apache. Managing databases with tables couldnt be much simpler by using phpmyadmin.

All you need to do is:

apt-get install phpmyadmin

The phpmyadmin configuration file is located at: /etc/phpmyadmin folder.

To set up under Apache all you need to do is include the following line in /etc/apache2/apache2.conf:

Include /etc/phpmyadmin/apache.conf 

Now restart Apache:

/etc/init.d/apache2 restart

Point your browser to: http://domain/phpmyadmin

That's it! MySQL and phpMyAdmin are ready. Log in with your mysql root password and create users to connect to database from your php script.

This tutorial was written and contributed to HowToForge by Scott who currently runs MySQL-Apache-PHP.com. Permission is fully granted to copy/republish this tutorial in any form, provided a source is mentioned with a live link back to the authors site.



LAMP On Red Hat

Linux, Apache web server, MySQL database and PHP scripting (LAMP) is very popular platform for web solutions. This tutorial shows how to install and configure Apache web server, MySQL database and scripting language. Linux, Apache, MySQL, PHP (LAMP) is very popular platform for web solutions. LAMP is Free, stable, easy to learn and well documented.

Apache Web Server

To install servers, you first have to become root with su -. Install the actual web server program

yum install httpd

If you don't have yum, you can use rpm -i httpd*.i386.rpm or apt-get install httpd.

Start web server, make it start automatically on boot

/etc/init.d/httpd start
chkconfig httpd on

Now your web server should be running. When you surf to http://localhost, you should see a test page.

mozilla http://localhost

Localhost usually works even without hole in the firewall. If you do not see your test page right away, click shift-reload (ctrl-shift-R or shift-F5) to bypass cache.

Hide the testpage, so that your computer does not look like a punching bag for wannabe crackers

echo "go away" > /var/www/html/index.html

Make a hole into firewall to make your web server visible to others.

iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
iptables-save > /etc/sysconfig/iptables

Check your ip number (it's not 127.0.0.1)

/sbin/ifconfig/

and surf to that address, for example http://10.0.0.1. If you see your test page, congratulations, you just installed your web server!

Other servers in Linux are installed just like above: install the program, start it and make it start automatically, and make a hole in the firewall.

User homepages

Web server content directory /var/www/html/ is not writable by normal users, and you don't want to edit web pages as root. That's why users create homepages in /home/foo/public_html. Homepages are shown in http://localhost/~foo

Allow users to create homepages

Edit web server configuration with your favourite text editor. If you don't have pico, you can install it yum install pine

pico /etc/httpd/conf/httpd.conf

Find the section about homepages by searching ctrl-W for public_html, then comment out the line with UserDir disable and remove the comment char "#" from the line UserDir public_html.

Activate your changes by restarting the web server.

/etc/init.d/httpd restart

Your users can now create homepages.

Create a homepage as a user

If you are still root (if you have a # in your prompt), exit.

Create a directory for homepages. Home directory, public_html and all directories under public_html must be executable (x) by all, so that web server can access files under them if it knows their name. Files must be readable by the web server.

cd $HOME
mkdir public_html
echo "my homepage" > public_html/index.html
chmod a+x $HOME $HOME/public_html
chmod a+r $HOME/public_html/index.html

If you have a lot of users (10+), automate this by putting public_html and index.html to /etc/skel and check default home dir permissions.

Browse to your home page. If your name is "user", it is located in http://localhost/~user. If you can see "my homepage", you are now a happy owner of a homepage.

PHP scripting

PHP is a powerfull scripting language with a C++ like syntax, many readymade classes, good examples and great documentation.

Install PHP scripting

yum install php

Because php is a module, you must restart a web server to load it.

/etc/init.d/httpd restart

Hello PHP World

As a normal user, write a sample php page

cd $HOME/public_html
pico hello.php

Write this sample code to hello.php


Text outside code block is printed normally to web page.

Usually there is a normal html web page outside the .

Browse to http://localhost/~user/hello.php. Put your own login name instead of user. If you see "Hello PHP World, 2+2 is 4", you have installed php and written your first program.

MySQL database

Install mysqld just like any server, but don't make a hole for it in the firewall. It is only used locally by Apache.

yum install mysql-server mysql /etc/init.d/mysqld start chkconfig mysqld on

Try your new SQL server

mysqlshow

+-----------+
| Databases |
+-----------+
| mysql |
| test |
+-----------+

Two databases. mysql contains database management system internal data, so don't edit it. test is a safe sandbox to play with.

mysqlshow test

Database: test
+--------+
| Tables |
+--------+
+--------+

If for some reason you do not have database test, create it CREATE DATABASE test; and then USE test;

No tables in database test yet. Let's CREATE some.

mysql

On the prompt mysql>, you can type mysql commands (USE, SHOW) or sql queries (CREATE, INSERT, SELECT).

USE test;
SHOW tables;
CREATE TABLE persons( name VARCHAR(50), email VARCHAR(50) ); SHOW tables;
DESC persons;
INSERT INTO persons VALUES('Tero Karvinen', 'karvinen iki.fi');
SELECT * FROM persons;
INSERT INTO persons VALUES('Sample Person', 'recycle@nosuch.invalid');
SELECT * FROM persons;
QUIT

After the two INSERTs, the last SELECT should return a table with two records

+---------------+---------------------------+
| name | email |
+---------------+---------------------------+
| Tero Karvinen | karvinen iki.fi |
| Sample Person | recycle@nosuch.invalid |
+---------------+---------------------------+
2 rows in set (0.01 sec)

You have now installed a database management system, and you also know some SQL.

PHP program using MySQL database

A database by itself is not very usefull - it needs a user interface. To create a web interface to database, we use PHP scripting language.

yum install php-mysql
/etc/init.d/httpd restart

Let's write a simple program to display our database. Use pico $HOME/public_html/mysql.php to copy this script to a file


PHP database example - http://iki.fi/karvinen.

iki.fi, adapted from php.net
* See http://iki.fi/karvinen Linux Apache MySQL PHP tutorial. */

/* Connect to database */
$link = mysql_connect("localhost", "root", "")
or die("Could not connect : " . mysql_error());
print "Connected successfully";
mysql_select_db("test") or die("Could not select database");

/* Perform SQL query */
$query = "SELECT * FROM persons";
$result = mysql_query($query)
or die("Query failed : " . mysql_error());

/* Print results in HTML */
print "
\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
print "\t\n";
foreach ($line as $col_value) {
print "\t\t\n";
}
print "\t\n";
}
print "
$col_value
\n";
mysql_free_result($result);

/* Close connection */
mysql_close($link);
?>

Browse to http://localhost/~user/database.php. Use your own login name instead of user

0 comments:

Post a Comment

Advertisement

 

Copyright 2009 All Rights Reserved SITS