Thursday, April 28, 2011

Checking File Size and Disk Space in Linux

Linux comes with two commands that are useful for checking disk space usage.

df is used to check the space of mounted partitions
du is used to check the file sizes of files and directories

The most common use for df is to check partition free space. To make the output more human readable, use the -h option.

df -h

If you need to check on the size of a directory or its contents, use the du command. The most common options are -h (human readability), -a (display files and folders, not just folders) and -c (display a total size at the bottom).

cd /some/dir
du -ahc

For more advanced uses, run each command with the --help option for a list of additional options.

Saturday, April 2, 2011

Download a File From Linux Command Line Terminal

Getting a file from the internet using the command line is super easy. Simply cd to the directory you want:
   cd /var/www/images

And use wget to download the file you need:
   wget http://www.someplace.com/image3.jpg

That's it! Don't let it fool you, wget is a very advanced tool. For more usage examples go here:
   http://en.wikipedia.org/wiki/Wget#Using_Wget

Install an FTP server on Amazon EC2 LAMP

I've recently created my first server on Amazon EC2 (more on that soon) and I needed to move some stuff over. Here's how to setup a secure FTP server and configure it for your LAMP.

First, install the ftp software:
   apt-get install vsftpd

Next, edit /etc/vsftpd.conf and uncomment or change the following:
   listen=YES
   anonymous_enable=NO
   local_enable=YES
   write_enable=YES
   connect_from_port_20=YES
   guest_enable=NO

Add following lines at the end of file:
   pasv_enable=YES
   pasv_min_port=1044
   pasv_max_port=1048

Next, you'll want to create a local user and a home directory where files will be saved. This keeps your www directory safe from intrusion. Upon connecting over FTP you'll be able to see and read from anywhere on the system, but only upload to one directory.
   adduser ftp_user
   passwd ftp_user
   mkdir /home/ftp_user
   chown ftp_user:users /home/ftp_user

You might only need the first command as your distro will take care of all the rest by prompting you.

Now you need to restart the ftp server to load the new configuration:
   /etc/init.d/vsftpd restart

If you are wondering why I chose to restrict the passive ports to 1044 - 1048, this is because Amazon blocks all ports unless specifically allowed by the security group. You will need to open those ports in your Amazon Console. Click Security Groups, chose your group, click the Inbound tab and create a new Custom TCP rule. Specify ports 1044-1048, click Add Rule and Apply Rule Changes.

Presto! You can now connect to your LAMP over FTP using ftp_user. Your files will be uploaded to /home/ftpuser

Tuesday, March 29, 2011

Multiple domains and subdomains in Apache2

If you want to host multiple websites and have one server running Apache2, it's pretty simple to setup virtual hosts so that each domain and subdomain will show different content. You'll need to point your domains and subdomains to the IP address of your server first, as DNS takes some time to update. Once that is done it's just a matter of configuring Apache.

You'll want to find your sites folder, it should be /etc/apache2/sites-available  Here you can either edit the default file or create your own. The advantage to creating your own is being able to enable/disable parts of your configuration rather than the whole thing. Either way, below is what you need for each domain/subdomain you wish to configure:

<VirtualHost *>
        DocumentRoot "/var/www/subdomain/"
        ServerName subdomain.cezary.com

        <Directory /var/www/subdomain/>
                Options Indexes FollowSymLinks MultiViews +Includes
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>


What does all that do? I'm glad you asked.

DocumentRoot  is the local path to your content
ServerName  has to match the domain or subdomain of your site
<Directory ...>  contains options and settings for your site. The ones listed here are pretty standard. Modify these to your liking.

You'll want to make a new <VirtualHost *> entry for each domain and subdomain you host. If you put this configuration in the default file then you are almost done. If you made your own file then there is one more step. You need to enable your custom configuration file. You can do this with the following command (as root):

a2ensite xxFILExx

Where xxFILExx is the name of your own config file. And that's it! Now you just need to tell Apache to reload the new settings.

/etc/init.d/apache2 reload

Enjoy!

Friday, March 25, 2011

Generating QR codes

To generate a QR code on your page simply insert the following:

<img src="http://chart.apis.google.com/chart?cht=qr&chs=100x100&chl=xxxURLxxx" width="100" height="100" />

Replace xxxURLxxx with the link you want. You can also change the size, but 100 by 100 is usually large enough to accommodate most URLs.

If you want to dynamically generate the QR code, you can replace xxxURLxxx with PHP like so:

<img src="http://chart.apis.google.com/chart?cht=qr&chs=100x100&chl=<?php echo $varURL; ?>" width="100" height="100" />

Wednesday, March 9, 2011

How to install cURL

If you are making your first shopping cart, crawler or simply need cURL to make existing code work, here's how to get it.

First things first, check to see if cURL is already running. Create the following PHP page:
<?php
   phpinfo();
?>
Visit that page and search for "curl". If nothing is found, you don't have cURL. A proper installation should have a reference under "Additional .ini files parsed" as well as a section called "curl" with all the details.

There are several ways to install/enable cURL, depending on your setup. Here are the two most common:

LAMP (as root or using sudo)
apt-get install curl libcurl3 libcurl3-dev php5-curl php5-mcrypt
XAMPP (edit xampp\apache\bin\php.ini and uncomment or add this line)
extension=php_curl.dll
In either case you'll need to restart Apache to load cURL after the installation.

To verify the installation, go to the PHP page you created and search for "curl".

Friday, March 4, 2011

Manage Your Code Deployment: Subversion To Web Server In One Click

Here's the simplest way to push a folder out of your repository onto your web server for testing.

<?php
$result = array();
$command  = 'svn export ';
$command .= 'https://your_server/svn/project /path/to/www ';
$command .= '--username USER --password PASS --force ';
$command .= '--non-interactive --trust-server-cert';
exec($command, $result);
print_r($result);
?>

- Host name HAS to match certificate (where I have "your_server")
- check .htaccess files, mod rewrite, ensure permissions are 755 and www-data is owner
- Version # can be forced with -r switch (svn -r 5 export ...)