Sunday, March 25, 2007

PHP+PostgreSQL set up

I spend a whole weekend to work on LAPP. Just a little different from the popular term of "LAMP"(Linux+Apache+MySQL+PHP), 'cause mine is Linux+Apache+PHP+PostgreSQL :p

Here is the my install log for the whole staff (I set up a server in VMware, so I can check my results and fix bugs before submitting to a public server).

1. Download Ubuntu
2. Create a new ubuntu operation system in VMware, boot from Ubuntu6 iso file
3. After the system is done, use apt-get command or package management to install Apache2, PHP5, PostgreSQL8. The following additional modulates should be included:

  • OpenSSL: a SSL server enable me to login to the system from remote
  • install modulates: php5-mhash, php5-gd, php5-curl, php5-mhash, php5-pgsql, php5-xslt.

4. Configure PostgreSQL setting:

  • $useradd postgres
  • $chown -R postgres:postgres /etc/postgresql
  • $ sudo nano /etc/postgresql/8.1/main/pg_hba #add 127.0.0.1 to trust#
  • $ sudo -u postgres createuser -P #create the account and input name, passwd.... #
  • $ sudo -u postgres createdb test #create a db named test #
  • $ psql -U [username] test #log into test db if everything goes right#

5.Networking config

6 files are involved:

· /etc/hosts (signed computer name to IP address)
· /etc/networks (associate domain with IP address)
· /etc/sysconfig/network (open or close network, config computer name and gateway)
· /etc/resolv.conf (add DNS server IP)
· /etc/rc.d/rc3.d/S10network (as a wizard to activate the ethnet)
· /etc/sysconfig/network-scripts

Configure IP, directly edit /etc/network/interfaces,static IP can be edited as following:auto lo eth0iface lo inet loopbackiface eth0 inet staticaddress 192.168.2.2 netmask 255.255.255.0 broadcast 192.168.2.255 gateway 192.168.2.1

If assigned by DHCP, edit as: auto eth0iface eth0 inet

Command line is a better way:

ifconfig -a # display the network confi# ifconfig eth0 inet down #stop network # eth0ifconfig eth0 inet up 192.168.1.2 \netmask 255.255.255.0 broadcast 192.168.1.255 #config IP address netmask # add default gw 192.168.1.1 eth0 #gateway config#

When done, restart the networking: /etc/init.d/networking restart

6. Test web server

Change to the directory /var/www/ create a file, named info.php, add to it. Type the URL : 127.0.0.1/info.php in a browser, php config info will be displayed.

Create a file named pg_connect.php, add text:

//connect pgsql test//

$linkid = @pg_pconnect("host=localhost dbname=test user=postgre password=secret")

or die("Could not connect to the PostgreSQL server.");


$result= @pg_query("SELECT* FROM mytable") or die("The query failed");

//out put the result//

while($row=pg_fetch_object($result)) echo "No.$row->id is ($row->name)
";

?> # this will check PHP+PostgreSQL successuful or not #


7. Configure virtual host for different domain

/etc/apache2/sites-available/default is the default rule for domain root.
Add my own default domain config:
DomainName www.mydomain.com
DomainAdmin webmaster@mydomain.com
DomainRoot /anywhere I want/

If want to run more than one domain, I have do create a new file in the fold:
nano ./sites-available/newdomain
in the file, add another rules:

ServerName www.newdomain.com
ServerAdmin webmaster@newdomail.com
DocumentRoot "/another location you want/"
ErrorLog "/var/log/apache2/***_errors.log"
CustomLog "/var/log/apache2/***_accesses.log" common


When separate every domain config rule into a single file, we can just control the host very easily:
sudo a2ensite [rule.filename] to enable a domain
sudo a2 dissite [rule.filename] to disable a domain
don't forget to restart apache for new rules:
sudo /etc/init.d/apache2 restart


4 comments:

Unknown said...

Hey Victor, awesome post! thanks for taking the time to capture and publish the process - I quite like the extra hard-core geek factor of virtualising your UBUNTU install through VMWARE.

When I find a spare hour or so I'll follow your guide and report back any speed bumps ;)

Anonymous said...

Wow, dude. That's hardcore.

(Victor) Xi Wang said...

hi, guys, thanks for comment here.
I will apologize if my log will lead you to spend extra time on these 'hardcore' things :( Anyway, We are on DMT project,right? We may focus on MEDIA things... So please forget it at the moment ;)

Jules said...

My God! Reading your blog makes me look forward to see your final project. Good luck, Victor!