Cron not running a PHP script with fwrite()

0 comments

Posted on 22nd October 2011 by admin in Random

, , ,

I recently created a very simple PHP script to just check a server every minute and see if it’s online. I wasn’t too bothered about whether it was or not, but recently I’ve had issues connecting to it, and wanted to check if it was a problem with the box or my ISP.

Anyway, in the PHP script, I had some code

// if website is down
$log = fopen('uptime.log', 'a');
fwrite($log, 'SERVER IS DOWN @ ' . date('H:i:s l jS F') . "\n");
fclose($log);

If I ran this script via Firefox, it worked absolutely fine and logged it to the log file. I set up Cron to run the script every minute, and it appeared to run the script (I tested it with another script) fine, so I couldn’t see what the problem is.

After some Googling, and trying a few different solutions, I finally discovered that the problem was with fopen() and the path I was using

I had only referenced the file uptime.log when really it should have been as below

$log = fopen('/full/path/to/file/from/root/uptime.log', 'a');

Note how I have the full path to the file. I’m still not sure why it didn’t work before through cron, and it did through the browser, but all that matters is it works now.

How to use a VPS/server as a proxy in 5 minutes with PuTTY

0 comments

Posted on 22nd October 2011 by admin in Random

, , , , , , , , , , ,

Intro

Okay. So, let’s say, just as an example, that you want to watch full episodes of your favourite TV show on Hulu, but you’re not from America. Well, Hulu will check the location of your IP address, find out you’re not in America, and then kindly inform you to go away. The same can be said for BBC iPlayer in the UK, and many other websites around the World.

However, let’s pretend you have a box in one of those countries with shell access! “I know! I can set up a PHP proxy and visit the website through that!” I hear you say.

Well…no, you can’t. First of all, PHP proxies suck, and it won’t work anyway. What you can do is configure your browser to route your traffic through your box via PuTTY, so all of your requests are coming from the IP of your box, and if it’s located in the same country, the website will allow it.

Software

No extra software will be installed on your server.

You will need the following software on your machine (you can download the portable executabl files so you don’t need to install it)

  • PuTTY
  • Firefox (or any browser if you know how to configure it, but this tutorial is for Firefox)

Configuring PuTTY

After downloading the software, you will need to configure it, so open up PuTTY and enter your server IP address in. On the left, double click SSH and then click on Tunnels

On the right, it should give you some different options, so where it says Source port enter 8345 and where it says Destination, choose Dynamic

Click add, and under Forwarded ports, it should say D8345

Click on Session on the left, then click on Default Settings and then Save

This will save your session so it’s easier next time. Anyway, you have it set up correctly now, so just hit Open and log in

Configuring Firefox

This can most likely be configured in other browsers, but this tutorial is for Firefox.

Open up Firefox, go to Tools, then Options, then Advanced, then Networks and Settings

Click Manual proxy coniguration then next to SOCKS Host enter 127.0.0.1 and next to Port enter 8345

Click OK as many times as you need to, and that’s it! You need to stay logged in to PuTTY while you’re browsing for it to work.

How to repair a damaged USB drive

0 comments

Posted on 21st October 2011 by admin in Random

, , , , , , , , , , , ,

Recently, I used Rohos Mini Drive on an old 1GB Freecom USB drive I have. Rohos uses up half of the space, so left me with just under 500MB for storing my documents. When I later reformatted the drive, it was now only giving me 500MB total (after removing Rohos and completely reformatting it)

Windows would recognize the other 500MB but not allow it to be formatted, and I couldn’t find  any solutions on the internet about how to get it back, or any other tools on the internet that would be able to get it back either.

Then today, I found HDDGURU’s HDD LLF (Low Level Format Tool) and after 2-3 clicks, it had fully formatted the USB drive in a few seconds (using quick format) and given me all of the space back

It’s completely free for personal use as well, the link is below

HDDGURU HDD LLF

How to set up Lighttpd + PHP + MySQL on a Debian server

0 comments

Posted on 17th October 2011 by admin in Random

, , , , , , , ,

Intro

I’ve seen a lot of people who have been put off getting a VPS or dedicated server because they’re not sure how to set up their website on it without paying extra for a control panel.

This is a quick guide to show how easy it is to get a PHP/MySQL website running on a Debian server with no control panel.

Software

The following software will be installed on your server

  • Lighttpd
  • PHP
  • MySQL
  • phpMyAdmin + mcrypt (optional)

You will need the following software on your machine (you can download the portable executable files so you don’t need to install them)

Connecting to your server

After downloading the software, you will need to log into the server via SSH, so open up PuTTY and enter your server IP address in. Leave the port as 22.

You will be prompted with the following text

login as:

so enter root then press enter and enter your password. Press enter again. You will then see the following

root@hostname:~#

Downloading the software

This means you’re logged into your server, via SSH, as the user root. The next step is to update the repositories for APT, which is basically a software package manager, update other packages on the server, and then install the software required, which is Lighttpd, PHP (FastCGI) and MySQL. Enter the following text at the command prompt in PuTTY (to paste in PuTTY, just right-click)

apt-get update && apt-get dist-upgrade && apt-get -y install lighttpd php5-cgi mysql-server php5-mysql

It will update APT, and then download the software (the -y switch is to skip confirmation). When it gets to installing MySQL, it will ask you to enter a password, and then confirm it. Choose something secure!

As soon as this finishes (it should only take a minute or two) your server will already be accessible on the web, but we haven’t configured PHP yet, so don’t upload any PHP files! To see your server, just put the IP address into your web browser (which hopefully will be Firefox!)

Once you’ve done that and been amazed at how easy it is (or just shrugged because you don’t care and just want to get on with uploading your site) we’ll get onto configuring Lighttpd to use PHP.

Configuring Lighttpd to use PHP

You can do the following using nano or some other command-line text editor if you’re familiar with it, but WinSCP is easier if you’re not.

Load up WinSCP (which you downloaded earlier) and enter your IP address, along with root as the username, and your password

Once you’ve logged in, navigate to the following path in the window on the right (using the folder icon with the arrow pointing upwards)

/etc/lighttpd/

In this folder, you will have a file called lighttpd.conf so double click on it, and it will open with Notepad. At the top of the file, you will see the following

server.modules = (
"mod_access",
"mod_alias",
"mod_compress",
"mod_redirect",
# "mod_rewrite",
)

Add "mod_fastcgi", above # "mod_rewrite", so it now displays

server.modules = (
"mod_access",
"mod_alias",
"mod_compress",
"mod_redirect",
"mod_fastcgi",
# "mod_rewrite",
)

When you’ve done this, scroll to the bottom of the file, copy and paste the following text into the file (right-click in PuTTY to paste)

fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/bin/php5-cgi",
"socket" => "/tmp/php.socket",
"max-procs" => 2,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
)))

After pasting that, save the file by clicking on the floppy disk at the top of Notepad, then close it and go back to PuTTY. Type in the following

/etc/init.d/lighttpd restart

This will restart Lighttpd, and it will now process PHP files.

Testing

You can test this by uploading a PHP file into the public HTML directory in WinSCP. The directory to store your files in is

/var/www/

Create a new file in WinSCP in the folder above called phpinfo.php, and enter the following text inside it

<?php phpinfo(); ?>

Then save and close the file. Open up your web browser (if it wasn’t Firefox before, hopefully you downloaded Firefox and now it is!) and go to the following address

http://your-ip-address/phpinfo.php

You should see a file with loads of text that your probably don’t understand. That is good. If you just see <?php phpinfo(); ?> that isn’t so good, so post a comment on here and I will take a look.

Setting up a database via command-line

The next step is creating a database, which you can either do via command-line (which is very easy) or installing phpMyAdmin (which takes a bit longer but is nice if you’ve just come from a shared hosting environment using cPanel or DirectAdmin and are familiar with it, or you just like doing things through a GUI.

To create a MySQL database via command-line, just type the following into PuTTY

mysqladmin -u root -p create databasename

It will prompt you for your MySQL password, so just enter it and your database will be created!

Setting up phpMyAdmin (optional)

Creating a database through command-line is so easier, but some people might prefer to use phpMyAdmin, so just follow these easy steps.

Go to the phpMyAdmin download pageand download the English version in .tar.gz format, then upload it to /var/www/ using WinSCP, or use wget if know how to get the direct link and are familiar with it. When you have done that, go to PuTTY on your machine, and type the following

cd /var/www/

This will change the directory to your web folder, where you should have the phpMyAdmin file located (phpMyAdmin-3.4.6-english.tar.gz as of writing this). Type the following to extract the folder

tar -zxvf yourfilename.tar.gz

If you install phpMyAdmin, you will also need Mcrypt so type this in as well

apt-get install -y php5-mcrypt

Then, open up WinSCP, and rename the folder to whatever you want. For example, the folder might be called phpMyAdmin-3.4.6-english – you can rename this to phpmyadmin or simply phpma

Finished

That’s it! You’re now ready to upload your scripts to /var/www/ and install them like you would before. If you noticed any errors, or it isn’t working, just leave a comment!