Wednesday, March 27, 2013

How do I enable or disable an apache virtual host?

To enable an apache virtual host run the following command:
sudo a2ensite example.com
To disable an apache virtual host run the following command:
sudo a2dissite example.com
Both of the examples above assume you're interested in enabling/disabling the site configured via the following file:
/etc/apache2/sites-available/example.com
Lastly, if you're interested in learning more about the a2ensite/a2dissite command, checkout the following notes:
#This manual page documents briefly the a2ensite and a2dissite commands.

#a2ensite  is a script that enables the specified site (which contains a
#<VirtualHost> block) within the apache2 configuration.  It does this by
#creating   symlinks   within   /etc/apache2/sites-enabled.    Likewise,
#a2dissite disables a site by removing those symlinks.   It  is  not  an
#error  to  enable  a  site  which is already enabled, or to disable one
#which is already disabled.

#The default site is handled specially: The resulting  symlink  will  be
#called 000-default in order to be loaded first.

Tuesday, March 26, 2013

How do I list all running processes and the amount of memory/cpu each is consuming on a linux machine?

First, start off by listing the processes that are running on your machine (i.e. their names). You can do so via the following command:
ps -e
After running the above command you'll see a list of running process and their names. Find the one you're interested in (or the one you think you're interested in) and then use its name in the following command:
ps -U www-data -u www-data u
In this case we used the name 'www-data'. You should see the instances of that process listed and the percentage of memory/cpu each is using.

Lastly, you may be wondering what the 'ps' command does (other than what you've observed here). If you run the following command, you can read all about it:
man ps
Lastly (again), the 'top' command is really awesome. It does the same stuff as the commands above but in real-time:
top

Saturday, March 23, 2013

What's the easiest way to get the latest version of ruby installed on my local *nix machine?

Using RVM (Ruby Version Manager). You can checkout the official site here: https://rvm.io/.

In short though, you can install ruby (and gems such as rails) via the following command:
curl -L https://get.rvm.io | bash -s stable --rails

Friday, March 22, 2013

How do I install a command line tool after uncompressing it?

First, you need to cd into the directory created as a result of uncompressing via tar. Something along the lines of:
cd path-to-software/
Once in the directory, run the following command:
./configure
"./configure" will configure the software to ensure your system has the necessary functionality and libraries to successfully compile the package.

If you're doing this on a Mac and you get a missing "c compiler" type error then you probably just need to install Xcode's command line tools package. To do so, simply open Xcode, go to preferences then downloads. There is an "Install" link to add the command line tools. Once this has been completed, make sure to re-open your terminal window.

Then run:
make
"make" will compile all the source files into executable binaries.

The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them.

Then run:
sudo make install
From the documentation, "make install" will copy the executable file into a directory that users typically search for commands; copy any auxiliary files that the executable uses into the directories where it will look for them (e.g. /usr/local/bin/).

Lastly, most software packages have a README or INSTALL file you should probably take a look at. You can do so by running the following command:
cat README

How do I uncompress a tarball?

To uncompress a tarball, execute the following command(s) depending on the extension:
tar zxf file.tar.gz
tar zxf file.tgz
tar jxf file.tar.bz2
tar jxf file.tbz2
The following doc snippets explain the tar options being used:
#Extract: tar -xf <archive-filename>
#-z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma
Lastly, if you were to run "tar zxf file.tar.gz" you would end up with a new directory named "file" in your current directory.

Monday, March 18, 2013

How do I get and install a trusted SSL cert on a linux machine?

It starts with understand the basic pieces of the puzzle.

In SSL there's a concept of a "key pair". A "key pair" is composed of a "private key" and a matching "certificate signing request".

So, to get started, you need to create a private key. Yup, it's all you and it's as simple as running a single command. The private key is created via:
openssl genrsa -out www.yourdomain-example.com.key 2048
Then, after you've created your private key, you can proceed to creating your certificate signing request. Your "CSR" is created via:
openssl req -new -key www.yourdomain-example.com.key -out www.yourdomain-example.com.csr
This is the point in the process where you load your SSL cert with info about your company. You can read the following article if you have questions about what to enter for each prompt: https://knowledge.geotrust.com/support/knowledge-base/index?page=content&id=AR876

A public/private key pair has now been created. The private key (www.yourdomain-example.com.key) is stored locally on the server machine and is used for decryption. The public portion, in the form of a Certificate Signing Request (www.yourdomain-example.com.csr), will be used for certificate enrollment. System admins need to submit their www.yourdomain-example.com.csr files to a "trusted" signing authority such as VeriSign or GeoTrust. The trusted source will then respond with a related "public key". Information encrypted with a public key can only be decrypted with the corresponding private key, and vice-versa.

Okay cool, that makes sense and all but I've heard that intermediate certificates are involved too… What gives? Well, first things first, we should establish the purpose of an intermediate certificate. The purpose of an intermediate certificate is to provide maximum browser and server coverage to ensure visitors won't receive "invalid SSL" warnings when they visit your site. The "trusted sources" intermediate certificate bundle "chains" your SSL certificate to their trusted root certificates, letting your certificate secure connections with older browsers that might have only an old root certificate installed. So yes, you should be getting an intermediate certificate from your chosen certificate signing authority.

Installation of the cert depends on your web server software. Checkout the following GoDaddy support article that does a great job of describing how to install the cert depending on the web sever software you're working with here: http://support.godaddy.com/help/article/5346/installing-an-ssl-server-instructions?locale=en

What's the purpose of each directory in a standard linux install?

Run the following command:
man hier
and you'll have something like this that prints to your screen:
HIER(7)                                                                                                                Linux Programmer's Manual                                                                                                                HIER(7)

NAME
       hier - Description of the file system hierarchy

DESCRIPTION
       A typical Linux system has, among others, the following directories:

       /      This is the root directory.  This is where the whole tree starts.

       /bin   This directory contains executable programs which are needed in single user mode and to bring the system up or repair it.

       /boot  Contains static files for the boot loader.  This directory only holds the files which are needed during the boot process.  The map installer and configuration files should go to /sbin and /etc.

       /dev   Special or device files, which refer to physical devices.  See mknod(1).

       /etc   Contains  configuration  files  which  are local to the machine.  Some larger software packages, like X11, can have their own subdirectories below /etc.  Site-wide configuration files may be placed here or in /usr/etc.  Nevertheless, programs should
              always look for these files in /etc and you may have links for these files to /usr/etc.

       /etc/opt
              Host-specific configuration files for add-on applications installed in /opt.

       /etc/sgml
              This directory contains the configuration files for SGML and XML (optional).

       /etc/skel
              When a new user account is created, files from this directory are usually copied into the user's home directory.

       /etc/X11
              Configuration files for the X11 window system (optional).

       /home  On machines with home directories for users, these are usually beneath this directory, directly or not.  The structure of this directory depends on local administration decisions.

       /lib   This directory should hold those shared libraries that are necessary to boot the system and to run the commands in the root file system.

       /media This directory contains mount points for removable media such as CD and DVD disks or USB sticks.

       /mnt   This directory is a mount point for a temporarily mounted file system.  In some distributions, /mnt contains subdirectories intended to be used as mount points for several temporary file systems.

       /opt   This directory should contain add-on packages that contain static files.

       /proc  This is a mount point for the proc file system, which provides information about running processes and the kernel.  This pseudo-file system is described in more detail in proc(5).

       /root  This directory is usually the home directory for the root user (optional).

       /sbin  Like /bin, this directory holds commands needed to boot the system, but which are usually not executed by normal users.

       /srv   This directory contains site-specific data that is served by this system.

       /tmp   This directory contains temporary files which may be deleted with no notice, such as by a regular job or at system boot up.

       /usr   This directory is usually mounted from a separate partition.  It should hold only sharable, read-only data, so that it can be mounted by various machines running Linux.

       /usr/X11R6
              The X-Window system, version 11 release 6 (optional).

       /usr/X11R6/bin
              Binaries which belong to the X-Window system; often, there is a symbolic link from the more traditional /usr/bin/X11 to here.

       /usr/X11R6/lib
              Data files associated with the X-Window system.

       /usr/X11R6/lib/X11
              These contain miscellaneous files needed to run X;  Often, there is a symbolic link from /usr/lib/X11 to this directory.

       /usr/X11R6/include/X11
              Contains include files needed for compiling programs using the X11 window system.  Often, there is a symbolic link from /usr/include/X11 to this directory.

       /usr/bin
              This is the primary directory for executable programs.  Most programs executed by normal users which are not needed for booting or for repairing the system and which are not installed locally should be placed in this directory.

Thursday, March 14, 2013

When I create a new file or folder, what determines the default set of permissions?

Your environment's umask setting. Default is typically
umask 022
which results in directories and files getting created with 755 and 644 permissions respectively.

When I create a new file or folder, what determines the group that owns it?

The new file or folder will be owned by the same group that owns the directory that you're adding it to (tested on Mac OS X).

How do I make it so all future files and dirs have the permissions I want (for example, 775 rather than 755 for new directories) on a linux machine?

On your linux machine, open the following file via the following command:
sudo vim /etc/pam.d/login
Then, add the following line to it:
session optional pam_umask.so umask=0002
Then, restart the machine.

How do I make it so all future files and dirs have the permissions I want (for example, 775 rather than 755 for new directories) on a Mac OS X machine?

On your Mac OS X machine, create or open the following file via the following command:
sudo vim /etc/launchd-user.conf
Then, copy and past the following text into it:
umask 002
Then, restart your computer.

Sunday, March 10, 2013

What does PHP's print_r($_FILES) look like?

<?php
print_r($_FILES);
/*
THE ABOVE CODE OUTPUTS:
Array
(
    [image] => Array
        (
            [name] => aircraft war military fighter pilot cockpit planes vehicles jet aircraft 3000x1928 wallpaper_www.wallpaperhi.com_6.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpzqf0Y6
            [error] => 0
            [size] => 383379
        )

)
*/

Saturday, March 9, 2013

How do I increase PHP's resource limits?

Check/edit your php.ini file for the following settings:
; Maximum amount of memory a script may consume (8MB).
memory_limit = 256M

; Maximum size of POST data that PHP will accept.
post_max_size = 32M

; Maximum allowed size for uploaded files.
upload_max_filesize = 32M

Friday, March 8, 2013

What does PHP's getimagesize function return?

<?php
$getimagesize = getimagesize('example/image/path/file.png');
print_r($getimagesize);
exit;
/*
THE ABOVE CODE OUTPUTS:
Array
(
    [0] => 1748
    [1] => 1165
    [2] => 2
    [3] => width="1748" height="1165"
    [bits] => 8
    [channels] => 4
    [mime] => image/jpeg
)
*/

How can I add an existing user to an existing group via the command line on a Mac OS X machine?

dseditgroup -o edit -u your_admin_username -p -a the_username_of_user_you_want_to_add_to_group -t user the_name_of_the_group_you_want_to_add_user_to
If I wanted to add myself to the "www" group I would simply run the following command:
dseditgroup -o edit -u johnerck -p -a johnerck -t user www
Run the groups command to see that you've been added:
groups

Wednesday, March 6, 2013

How can I determine the differences between two MySQL databases?

mysqldump --skip-comments --no-data -u your_user1 -p your_database_name1 > file_1.sql
mysqldump --skip-comments --no-data -u your_user2 -p your_database_name2 > file_2.sql
diff file_1.sql file_2.sql
Note that the first two lines may end up being identical except for the .sql file name at the end of the command (because databases that you want to compare often exist in independent environments). For example, you might run the first command locally and the second on a remote dev machine. In both instances your user may be root and your database name might be the name of your project. If you run the first command on your local machine and the second on a remote, you'll have to scp file_2.sql to your local machine before running the diff command. Enjoy!

Monday, March 4, 2013

How do I search multiple files for a specific word in one fell swoop?

# To list files that contain "the phrase"
grep 'the phrase' -li -r /etc
# To search for "the phrase" within a specific file
grep 'the phrase' /etc/httpd/conf/httpd.conf
This command is particularily useful if you're searching a folder full of MS Word docs. Word docs aren't plain text files (duh) and therefore don't get matched properly by most application level search features (e.g. TextWrangler).

The 'l' option is shorthand for '--files-with-matches'. Only the names of files containing selected lines are written to standard output.

The 'i' option is shorthand for '--ignore-case'. Omit this option if you do want your search to be case sensitive.

Lastly, the 'r' option is shorthand for '--recursive'. This tells grep to recursively search the subdirectories listed. This is key when you're interested in searching a folder full of docs (as opposed to an individual file).

About Me

My photo
I code. I figured I should start a blog that keeps track of the many questions and answers that are asked and answered along the way. The name of my blog is "One Q, One A". The name describes the format. When searching for an answer to a problem, I typically have to visit more than one site to get enough information to solve the issue at hand. I always end up on stackoverflow.com, quora.com, random blogs, etc before the answer is obtained. In my blog, each post will consist of one question and one answer. All the noise encountered along the way will be omitted.