The PHP Socket Server
Step-by-Step on Linux

Return to Index

If you're reading this, then FlashChat is installed on your web server, but without the socket server option enabled. If it's not installed, stop reading this and install it.

Now, let's take this step by step.

Step 1: Enable sockets in PHP

When installing PHP, you should enable sockets using the --enable-sockets option, as shown below.

cd php-4.3.4
./configure --with-apxs=/usr/sbin/apxs --enable-track-vars --enable-sockets --with-config-file-path=/etc --enable-ftp --with-zlib --with-openssl=/usr/local/ssl --enable-force-cgi-redirect --enable-exif --with-gd

If sockets are not pre-compiled with PHP, you may have to dynamically load the socket extension, by uncommenting this line:

;extension=sockets.so

to this:

extension=sockets.so

Then, restart your server. To verify that the socket server is running correctly, create a new php file with the following contents:

<?php phpinfo(); ?>

Then, put this file your FlashChat folder, and open it with a web browser. You should see a blue page listing your PHP settings. If the socket server is running as it should, you will find this section:

If you don't see this, then STOP and contact your web host, or at least someone who is highly proficient in PHP/MySQL setup. The socket server will not work until this option is correctly enabled.

Step 2: Set FlashChat's /inc/config.php

Now, open FlashChat's config.php file, which is located in the "inc" folder, with a good text editor. Locate this line, and change it from "false" to "true". When editing any PHP files in FlashChat, be careful of syntax errors, and do not add any extra spaces or carriage returns after trailing "?>" symbols.

        'enableSocketServer' => true,

If you are using the Java Socket Server please refer to that documentation: LINK

Step 3: Set host configuration

Open the "config.socketSrv.php" file, located in the /inc/patServer folder of FlashChat. Set the "host" property to the domain of your server, like "your_domain.com" or the IP address of your server. I tested this on tufat.com, so I used "tufat.com", although the IP address of tufat.com would also work. Leave all other settings as they are. 

If your website is www.your_domain.com then you need to put in www.your_domain.com otherwise the socket server will not accept connections.

If you are using a firewall, you may need to open the port that the socket server is listening on. By default, this is port 9090.

Step 4: Edit and CHMOD fcssd

Change the current working directory to the /inc/patServer/linux folder (within FlashChat), and chmod the "fcssd" file so that it's executable.

Make the following two edits in the fcssd file, using any text editor:

a) Set php_dir to the PHP executable directory path:

php_dir="/usr/local/bin"

If you don't know where the PHP executable on your server is, you can use the "whereis" Linux command. For example, "whereis php". You'll get a list of all files named PHP in the system directories of your sever. Usually the first item is the right path to the PHP executable. Most likely, it wil be at "/usr/local/bin" if PHP was installed by compiling it from the sources. In the case of Linux Redhad/Fedora as in the below screenshot, the PHP executable is located at "/usr/bin".

You need only the path to PHP, without the word "php" itself, so thte right php_dir value in this case would be:

php_dir="/usr/bin"

b) Set fcss_dir to the patServer directory path:

fcss_dir="/srv/www/htdocs/chat/inc/patServer"

Please ensure that there is no trailing "/" at the end of the fcss_dir path.

Step 5: Running fcssd

Now, change to the "linux" directory (/inc/patServer/linux in your FlashChat path), and run this command:

./fcssd

If you get this error, then it means that the "screen" package is not installed:

To install screen, do the following, first get the screen package from here. Of course, if you do not get this error, then skip to Step 6.

http://www.gnu.org/software/screen/screen.html

You'll download a file that looks like this (this was the current version as of March 3rd, 2006):

screen-4.0.2.tar.gz

Then, install GNU screen by doing the following. This is more or less how to install all GNU packages, although it can vary by the server. Probably best to have your system admin do this...

To unarchive the screen package, type:

tar -zxf screen-4.0.2.tar.gz

Change to the screen directory:

cd screen-4.0.2

Type:

./configure

to start the configuration of screen. Be sure that configure is executable.

Type:

make

to link, then

make install

to install.

Step 6: Auto-Starting the Server

After installation is finished, you will have to make the socket server auto-start. There are two ways to do this: using start-up scripts, or using a CRON job. The first metho dis a bit better, as you'll have the socket server started every time your web server is rebooted. You'll need to have administrative access to your server in this case. Starting a cron job doesn't require administrative access, but the server will try to restart after regular intervals of time.

Using Startup Scripts...

Implementation depends on your Linux system, however in many systems you can use the following (this is how it is for RedHat, Fedora, and Slackware). You need to add this line to /etc/rc.d/rc.local to auto-start the socket server:

/srv/www/htdocs/chat/inc/patServer/fcssd

NOTE: In this case, "fcssd" was located in "patServer", but depending on your version of FlashChat, it might be located in the "linux" subfolder, in which case that path might be something like:

/srv/www/htdocs/chat/inc/patServer/linux/fcssd

Of course, you should change this to the correct location of your fcssd script. The command to add may look something like this:

echo '/srv/www/htdocs/chat/inc/patServer/fcssd' >> /etc/fc.d/rc.local

You only have to change the path to where FlashChat is deployed. Another way to add this to your rc.local file is to use the vi editor to add the line. For example, type:

vi /etc/rc.d/rc.local

You'll see the vi interface with the rc.local file opened:

This requires basic knowledge of the vi program (vi docs). At the end of the file, add the line to fcssd:

/srv/www/htdocs/chat/inc/patServer/fcssd

then, use :wq to save and quit vi.

Now, reboot your server. The screen package will automatically start, but just in case, you can check this using:

ps -A|grep screen

Hopefully, you'll get a response that looks something like this:

5201 ? 00:00:00 screen

Using a CRON job...

This method is a bit easier to implement, but it means that the server is continually going to restart every X duration of time. Of course, if you already did this using the start up script method, then skip the CRON method.

Type:

crontab -e

to open the vi editor, and add the fcssd command at the end of the file, to start fcssd 20 minutes after every hour:

20 * * * * /srv/www/htdocs/chat/inc/patServer/fcssd > /dev/null

As usual, you'll have to type the correct path to the script. Press ESC then :wq to write the file.

You can start fcssd manually initially, by running ./fcssd again to start the socket server (you'll need to be located in the directory that fcssd is located). You should get "Socket server is runnning." in the command line.

Debugging

If you receive a "Socket server connection failed" error, then check your socketServer.log file for possible error messages. This file is located in the /inc/patServer folder of FlashChat. Also, re-check the above steps to ensure that syntax errors were not introduced at any point.

If you still are not sure what is wrong with the socket server first make sure it is correctly started by typing the following at a command prompt on your server:

        ps -A| grep -i screen

If you don't see a line similiar to the one shown below, then the server is not correctly started.

cowbud   25177  0.0  0.1   3924   956 ?        Ss   02:01   0:00 SCREEN -A -m -d /usr/bin/php -q ./runServer.php

If a line as above does appear, try to connect to the server from the client using telnet. This command will work either via a Unix command prompt, or with Windows from the C:\ command prompt.

        telnet www.your_domain.com 9090 

This should open a connection. If no connection opens then your client's connections are probably being blocked by a firewall.

More information about the socket server can be found in the Socket Server PDF file, which comes with your FlashChat download.

Thanks for using FlashChat, and good luck!
Darren