Jumat, 21 Agustus 2009

Lost in doing PHPSurvey?

Lost in implementing PHP survey?

That happened to me as of NOW. Yes, I'm writing this blog during my work on this script.

I've downloaded the script from Sourceforge, it's available thru http://sourceforge.net/projects/phpsurvey/, you can download it there.

First thing first, check several prerequisites:
- Apache, it's a must, and since I'm using Fedora 10 with complete package installed, it doesn't give hard time in setting it up. To check it, use this command:
/etc/init.d/httpd status
or simply press tab key after keying htt, linux will complete it for you if it's there, much like cisco routers right?
This is what shows up on my screen:
[root@vmfc10 lib]# /etc/init.d/httpd status
httpd is stopped
[root@vmfc10 lib]#
If you see the above lines, you need to start it this way:
[root@vmfc10 lib]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@vmfc10 lib]#
You see, there is an error about the unqualified domain name. It's ignoreable unless you'd wish to bring it online to the internet. It appears because my hostname was set to 'localhost' which does not meet with the FQDN rules ... but we're not gonna talk about it here, it's too long. For now, it's ignoreable.
After starting the httpd service, now you have to make sure that the web page can be displayed. By default, apache places one test page located under its default document root directory for us to use. Simply point your browser to http://localhost/ then it will show you one test page similar to this:


Okay, now finish for the apache.

- Move on to mysql.
Unfortunately, mysql is not installed by default in Fedora distributions so I have to make a little effort, only a little though, thanks to yum.

Please remember that the service you're searching for mysql service will be mysql and mysql-server. If you're only installing mysql, you will only see some mysql related libraries and will not be able to run it as a daemon.

Just do "yum install mysql mysql-server" as a root, please note the intermediating dash.
Once it said "Completed!" then we can start moving to the configuration.

As root, run mysql by typing /etc/init.d/mysqld start. And afterward, log in to mysql service as root and assign the password for the root:

[root@vmfc10 ~]# /etc/init.d/mysqld start
Starting MySQL: [ OK ]

Please note that to start mysqld service, it took you quite a while, depending on your hardware specification.

[root@vmfc10 ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.83 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password for 'root'@'localhost' = PASSWORD('r00t');
Query OK, 0 rows affected (0.18 sec)

The sql statement which says "set password ... " is the line that is used to assign password for the root user in the mysql database.

So far, we have already 2 things set up on our Fedora machine. I don't know about you, but for me, the PHP comes in a bundle when I installed the webserver using the distribution package. So when I do this:

[root@vmfc10 ~]# whereis php.ini
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib/php /usr/share/php /usr/share/man/man1/php.1.gz

I already got the php.ini file. Installing PHP in Linux will not be harder than when installing it in Windows. So, I believe you will get to it by yourself. The installation procedure is also quite self explanatory.


Now, we got all the prerequisites in place. Let's do the script.

First thing first, place the script on the httpd DocumentRoot. Open httpd.conf, usually it's located on /etc/httpd/conf/ -- its default installation directory, or if you can't find it, just do "locate" or "find" command.

Find the DocumentRoot directive to get the location to which we should place the PHPsurvey script. As for mine, it shows as below:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

So, we know now that we have to place it on the /var/www/html directory. I got mine copied into survey subfolder under html.

[root@vmfc10 ~]# cd /var/www/html/
[root@vmfc10 html]# ls
phpSurvey-2.0-alpha.zip survey

According to the installation guide, we have to make sure the register_global is on. It's located ini php.ini file, so change it to ON, by default, it's turned off:

; You should do your best to write your scripts so that they do not require
; register_globals to be on; Using form variables as globals can easily lead
; to possible security problems, if the code is not very well thought of.
register_globals = On

Next, configure the database. All the necessary statement are in pg_create.sql file. However, these statements are written for PostgreSQL which differs from the MySQL, so you have to translate it to the MySQL version.

First of all, open the mysql prompt:
[root@vmfc10 html]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.83 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

The -u specifies the user, and the -p specifies that it requires password. Once you're logged in, create a database and then use it before creating any tables.

mysql> create database survey;
Query OK, 0 rows affected (0.18 sec)

mysql> use survey;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

Now, paste the sql statement in pg_create.sql file, located under install subfolder. Mostly, it fits fine, only there are several things that does not go with the MySQL.

1. The TIMESTAMP WITHOUT TIME ZONE statement. I've been looking for this statement in the MySQL documentation but I can not find any, so the only way to make it work, is by deleting the WITHOUT TIME ZONE.

2. You will see an error that said TEXT/BLOB variable used as primary key does not have length defined. As it is used as primary key, of course it has to have a specified length to determine the uniqueness. However, when you set the length for TEXT variable, you will find an error as well. So the only way to work it by refining the TEXT/BLOB into VARCHAR and specified the length.

3. You will also see that BLOB/TEXT can not have a default value of NONE. It's a known bug in MySQL, you have to change it to '' not specifying anything as the default value and MySQL can accept it.

So, we have done configuring the database.

Now, what we're gonna do is tuning the configuration. All the site related configurations are accessible from conf.inc.php under include folder.

$PS_CONFIG["system_url"] = "http://127.0.0.1/survey"; // by default it has a value of http://127.0.0.1:8080/phpSurvey. I changed it that way because my server is listening to port 80, not 8080 and the folder name under /var/www/html is "survey" not phpSurvey.

$PS_CONFIG["admin_email"] = "root@locahost"; // Displayed on the admin login page.
$PS_CONFIG["system_email"] = "root@localhost"; // Email address required in sending invitation.
// Should be a valid email address or leave it empty.
// Also remember to appropriately set hostname system variable.

$PS_CONFIG["dbtype"] = "mysql"; // initially, it was pgsql
$PS_CONFIG["dbhost"] = "127.0.0.1";
$PS_CONFIG["dbuname"] = "root";
$PS_CONFIG["dbpwd"] = "r00t";
$PS_CONFIG["dbname"] = "survey"; // you also need to change the database to the one you set up previously.

Now the guide says I need to invoke install.php to set the password. So let's do it.

Oooppsss ... something went wrong, nothing is displayed. Time for debugging. Open the error.log and here we go:

[Fri Aug 21 23:09:54 2009] [error] [client 127.0.0.1] PHP Warning: include_once
(DB.php) [function.include-once]: failed to
open stream: No such file or directory in /var/www/html/survey/include/common.in
c.php on line 3
[Fri Aug 21 23:09:54 2009] [error] [client 127.0.0.1] PHP Warning: include_once
() [function.include]: Failed opening 'DB.php' fo
r inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/s
urvey/include/common.inc.php on line 3
[Fri Aug 21 23:09:54 2009] [error] [client 127.0.0.1] PHP Fatal error: Class 'D
B' not found in /var/www/html/survey/include/common.inc.php on line 16

Ow ow ... it turned out that I got no DB.php file under my include directory.

Well, it looks like I dont have pear installed. Download it from the web, http://pear.php.net/package/DB/download, extract the file and copy it to the phpSurvey directory. In this case, it will be /var/www/html/survey/.

Try it again ... and oooppssss ... still blank page.

Open error log one more time, and here it goes:

[Fri Aug 21 23:30:39 2009] [error] [client 127.0.0.1] PHP Warning: require_once
(PEAR.php) [function.require-once]: failed t
o open stream: No such file or directory in /var/www/html/survey/DB.php on line
30
[Fri Aug 21 23:30:40 2009] [error] [client 127.0.0.1] PHP Fatal error: require_
once() [function.require]: Failed opening require
d 'PEAR.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/
survey/DB.php on line 30

line 30 on DB.php is

require_once 'PEAR.php';

Hmmm, I got to also download it. Search the net ... bingo, I've gotta install php-pear, do yum install php-pear ... wait several minutes and completed.

Try again, .... great it shows an error now, the error shows that it failed when connecting to the DB. Well, at least it shows a progress.

Let's take a look at the install.php, what is it actually in there?

I think I've gotta take a rest for now, will continue tomorrow. See ya.

----

it's saturday, it's the first day of ramadhan in 2009 and it's time to continue this ... last time we tried, the page shows a DB connection failed.

Hmmm .... at this point ... let's think of something what might be preventing this ... reviewing all the DB configurations are correct. Hmm I got it, perhaps it's the SELinux thing. Let's go check it out.

Now I gotta disable the SELinux.

Now done, try to browse the install.php again ... and oopsss I got a notification of AVC denial. Check it ... and it looks like the network connection has been denied by SELinux.


See? I've gotta set something for the SELinux, let me resolve this one moment .. as you can see above, the httpd is trying to connect to port 3306 and SELinux has denied it.



Now .. I'm allowing all network access as seen above. I shuld be able to connect and browse the install.php file by now.
Great ... it shows me something here:


Magic Quotes OFF? Let's check the guide. It doesnt help. If we're looking at the code there are lines:

$reg_globals = ini_get('register_globals');
$magic_quotes = get_magic_quotes_gpc();
echo "Register Globals : ";
if ($reg_globals == 1) {
echo "On
\n";
} else {
echo "Off
\n";
}
echo "Magic Quotes : ";
if ($magic_quotes == 1) {
echo "On
\n";
} else {
echo "Off
\n";
}

I assume the reg global shuld be off and magic quotes shuld be on. Let's change it in php.ini and restart httpd. Wonderful, it's all green now.
Fill up all the credentials ... and great ... the root password has been set up successfuly. This credential will be used for the web admin, not for the database.

The INSTALL file stops here. So what's next?

Ow ... great ... it's all done. Point your browser to http://localhost/survey and it will ask you to login. Click admin login, supply your recently created username and password ... and there you go ..



Fiuuuu it doesnt stop here ... it looks like i got some errors due to mismatch statement. All the sql statement here is actually destined for postgre, not mysql. I think I gotta stop here :D, i dont want to crawling all the pages just to find which statements needed to be changed.

As an alternative, I'm gonna yum install postgresql in my linux.

Running Transaction
Updating : postgresql-libs-8.3.7-1.fc10.i386 1/3
Installing : postgresql-8.3.7-1.fc10.i386 2/3
Installing : postgresql-server-8.3.7-1.fc10.i386 1/1
Cleanup : postgresql-libs-8.3.4-1.fc10.i386 3/3

Installed:
postgresql.i386 0:8.3.7-1.fc10
postgresql-server.i386 0:8.3.7-1.fc10

Dependency Updated:
postgresql-libs.i386 0:8.3.7-1.fc10

Complete!

Done with the yum ... now let's start it over.

[root@vmfc10 ~]# /etc/init.d/postgresql status
postmaster is stopped
[root@vmfc10 ~]# /etc/init.d/postgresql start

/var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first.
[FAILED]
[root@vmfc10 ~]# service postgresql initdb
Initializing database: [ OK ]
[root@vmfc10 ~]# /etc/init.d/postgresql start
Starting postgresql service: [ OK ]

Ahhh ... one thing I forgot, I shuld install pgsql as well !!!

Installed:
php-pgsql.i386 0:5.2.9-2.fc10

Dependency Updated:
php.i386 0:5.2.9-2.fc10 php-cli.i386 0:5.2.9-2.fc10
php-common.i386 0:5.2.9-2.fc10 php-ldap.i386 0:5.2.9-2.fc10
php-mysql.i386 0:5.2.9-2.fc10 php-pdo.i386 0:5.2.9-2.fc10

Complete!

[root@vmfc10 ~]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
[root@vmfc10 ~]# post
postgres post-grohtml postmaster
[root@vmfc10 ~]# postgres
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
[root@vmfc10 ~]# exit
logout

[yuv@vmfc10 ~]$

Hemmmm .... what a work !!!! cant make it work. Im gonna take a break now .. see ya later.

Back in action. It turned out that I've made terrible mistake :D which shuld be resolved in just 3 simple steps. See below:

[root@vmfc10 ~]# mkdir /usr/local/pgsql
[root@vmfc10 ~]# mkdir /usr/local/pgsql/data/
[root@vmfc10 ~]# chown postgres /usr/local/pgsql/data/
[root@vmfc10 ~]# su - postgres
-bash-3.2$ initdb -D /usr/local/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale en_US.UTF-8.
The default database encoding has accordingly been set to UTF8.
The default text search configuration will be set to "english".

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers/max_fsm_pages ... 24MB/153600
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

postgres -D /usr/local/pgsql/data
or
pg_ctl -D /usr/local/pgsql/data -l logfile start

-bash-3.2$ pg_ctl -D /usr/local/pgsql/data/ -l logfile start
server starting

Revert back the pg_create.sql from the original archive.
-bash-3.2$ psql
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

postgres=# \i /var/www/html/survey/install/pg_create.sql

There are a lot of errors showing up ... but let it be for now. I keep a copy of it in case I'm gonna need it sometimes later.

.....

It's been several days since I left this draft. I must say, I do not finish this. Why? Because there are a lot of sql statements I gotta go thru, and it's one tedious work to do :D. So ... I decided to move my ass to LimeSurvey and now it's running live already.

Just need to customize the webpage a lil so it does not show a real limesurvey. Sorry guys to let you down by writing this but I got a lot more work to do :)

Tidak ada komentar: