Basic C++ Tutorial

We'd like to give our new category, Programming, a bit of love. So, we're going to have some C++ tutorials from the ground-up, starting with your classic Hello World.

This tutorial should work on any system with a C++ compiler. For Windows, try Dev-C++. Ubuntu and Debian users, install the package build-essential. Fire up your text editor or IDE and let's get going. The rest of this tutorial will be in the code comments (marked by /* */ and //) below; while you can copy them into your compiler if you want, they are not necessary.

I recommend manually typing in the code above, line by line (excluding the comments). This makes sure you are able to learn by doing and not just by seeing. Save the file you just wrote as test.cpp and then open a terminal (unless you are using Dev-C++, in which this can be accomplished by hitting Compile).

g++ -o test test.cpp

This invokes the compiler using the file test.cpp and outputs it as a test binary. To run your newly-made program:

./test

If it didn't compile correctly, check your error messages and look for common mistakes, such as a missing quote or semicolon. If it did work, congratulations! You've just written your very first C++ program. Subscribe to the Programming section for more tutorials like these in the future.

My server backup solution

I'd like to share something with you today and that is the backup solution that I use on my web server. My server powers my personal website and a couple of other small sites too.

Actually I lied earlier - what I'm going to share today is only part of my backup solution, which backs up the database and the files in my main website folder and the folders for each of my virtual hosts (the sites on subdomains of my main domain).

So let's dissect my solution quickly.

I have a shell script which performs these backups and it gets run nightly in the early hours of the morning using cron. What the script does is it takes a mysqldump of the database, uses tar to backup the relevant directories.

The backups are stored in /root/Backups, but the script also mirrors them to a secondary drive I also have in my server, so there are always two copies of every backup at any one time. If one drive fails, the backups should still be found on the other, is the theory.

The script also has one final cool feature - it automatically prunes out older backups from both locations, so I don't run out of disk space, but I still have plenty of backups saved in case I need to restore from one older than the most recent.

So that's all very well and good - but you want to see some code right? Well, it's your lucky day.

#!/bin/bash

DATE1=`date | awk '{print $3}'`
DATE2=`date | awk '{print $2}'`
DATE3=`date | awk '{print $6}'`

THEDATE="${DATE1}${DATE2}${DATE3}"

KEEPORIG=10
KEEPSECOND=30

KEEPSQLORIG=30
KEEPSQLSECOND=50

ORIGDIR="/root/Backups"
SECONDDIR="/secondarydir"
APACHEDIR="/var/www/html"
VHOSTSDIR="/var/www/vhosts"

MYSQLUSER=root
MYSQLPASS=pass

cd ${ORIGDIR} # chdir to the right dir
tar -cjvf "./MainApache_${THEDATE}.tar.bz2" ${APACHEDIR} # tar up apache dir
tar -cjvf "./Vhosts_${THEDATE}.tar.bz2" ${VHOSTSDIR} # tar up vhosts dir

mysqldump -u ${MYSQLUSER} -p${MYSQLPASS} -A > "./MySQL_${THEDATE}.sql" # dump db
bzip2 "./MySQL_${THEDATE}.sql" # compress db

cp -v "./MainApache_${THEDATE}.tar.bz2" ${SECONDDIR} # copy
cp -v "./Vhosts_${THEDATE}.tar.bz2" ${SECONDDIR} # copy
cp -v "./MySQL_${THEDATE}.sql.bz2" ${SECONDDIR} # copy

# prune .tar.bz2 in original folder
if [ `ls -1 "${ORIGDIR}" | grep .tar.bz2 | wc -l` -gt $KEEPORIG ]; then
i=1
for each in `ls -1t "${ORIGDIR}" | grep .tar.bz2`; do
if [ $i -gt $KEEPORIG ]; then
echo Removing "${ORIGDIR}/${each}"
rm -fv -- "${ORIGDIR}/${each}"
fi
let "i = i + 1"
done
fi

# prune .tar.bz2 in second folder
if [ `ls -1 ${SECONDDIR} | grep .tar.bz2 | wc -l` -gt $KEEPSECOND ]; then
i=1
for each in `ls -1t "${SECONDDIR}"`; do
if [ $i -gt $KEEPSECOND ]; then
echo Removing "${SECONDDIR}/${each}"
rm -fv -- "${SECONDDIR}/${each}"
fi
let "i = i + 1"
done
fi

# prune db dumps in original folder
if [ `ls -1 "${ORIGDIR}" | grep .sql.bz2 | wc -l` -gt $KEEPSQLORIG ]; then
i=1
for each in `ls -1t "${ORIGDIR}" | grep .sql.bz2`; do
if [ $i -gt $KEEPSQLORIG ]; then
echo Removing "${ORIGDIR}/${each}"
rm -fv -- "${ORIGDIR}/${each}"
fi
let "i = i + 1"
done
fi

# prune db dumps in second folder
if [ `ls -1 "${SECONDDIR}" | grep .sql.bz2 | wc -l` -gt $KEEPSQLSECOND ]; then
i=1
for each in `ls -1t "${SECONDDIR}" | grep .sql.bz2`; do
if [ $i -gt $KEEPSQLSECOND ]; then
echo Removing "${SECONDDIR}/${each}"
rm -fv -- "${SECONDDIR}/${each}"
fi
let "i = i + 1"
done
fi

If you want an easily downloadable version of said script, feel free to grab it here.

Enjoy!

Installing PHP for Lighttpd on Linux

So you've got your very own Lighttpd server up and running as per this tutorial, but you want to serve a little more than just static content.

PHP is the obvious choice here - it is by far the most popular solution for dynamic content and is almost ubiquitous in the FOSS world. So how do we get Lighttpd to serve PHP content?

In this tutorial I'm going to show you how to compile PHP yourself and set it up using Lighttpd's FastCGI wrapper.

Before we get started, I should point out that you can find a quicker tutorial of this process on the official Lighttpd site. My tutorial is going to expand on that process a little and explain it some more as we go through.

First things first, let's grab the PHP source code from the PHP website so we can compile it for our purposes. At the time of writing, the latest version is PHP 5.2.4, so let's grab that.

Once that's downloaded, let's extract:

$ tar xjvf php-5.2.4.tar.bz2

The next step is to configure the PHP distribution for use. If you have custom compile options you want to enable, feel free to add them in here. If not, just follow this command:

$ ./configure --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect

After that, it's the usual PHP install process:

$ make
$ su -c "make install"

(or sudo make install)

You should now have installed a FastCGI-ready PHP binary in /usr/local/bin/php-cgi. Now we need to edit our configuration file to tell Lighttpd to let this PHP handle all .php files. Sounds easy enough right?

So you now need to open up your Lighttpd configuration file in a text editor. If you followed the previous Lighttpd tutorial, that will be /etc/lighttpd/lighttpd.conf.

You need to add these lines:

server.modules = (
"mod_fastcgi",
) fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/local/bin/php-cgi",
"socket" => "/tmp/php.socket"
)))

Save the configuration file. Once you restart Lighttpd, you should be able to successfully run PHP scripts. To test that, make a file called phpinfo.php. In that file, write this:

<?php phpinfo();?>

Save that, place it in /var/www, and then browse to http://localhost/phpinfo.php. If you see something like this:

PHPInfo

Then you have successfully got PHP up and running - congratulations!

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6

Sign In

    Enjoy FOSSwire's content? Have it delivered! Subscribe