Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Misc. terminal command reference


# Creating symlink
ln -s   [destination file/dir]    [link name]

# Import svn 
svn import -m "initial import" . http://svn.cdnm.com/repo

# Untar all .tar files in current directory:
for i in *.tar; do tar -xvzf $i; done

# Creating an archive
$ tar cjf outputfile.tar.bz2 inputs
Extract said archive to the current directory. For a compressed archive, you’ll again need to add the z for a .tar.gz, or j for .tar.bz2.
$ tar xf inputfile.tar
$ tar xjf inputfile.tar.bz2

Helpful localhosting terminal commands

// description of your code here


# Adding vhosts
mate /etc/httpd/httpd.conf
mate /etc/hosts

# Restart Apache
sudo apachectl graceful

# Recursively remove .svn folders
rm -rf `find . -type d -name .svn`

Force traffic over HTTPS

// Force traffic over HTTPS to avoid weird session dropping issues, Also handles addition or removal of www prefix as needed for your security cert,


<IfModule mod_rewrite.c>

RewriteEngine On

  # Force removal of www
  RewriteCond %{HTTP_HOST} ^www\.(.+)$
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  # (Or force addition of www depending on your cert)
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

  # Now Force traffic to use HTTPS
  RewriteCond %{SERVER_PORT} !443
  RewriteRule ^(.*)$ https://securesiteurl.com/$1 [R=301,L]

</IfModule>

Remove sudo password request when deploying

Change the sudoers list by adding a NOPASSWD: [Cmnd_Alias] line.

In terminal: visudo

# Cmnd alias specification
Cmnd_Alias HTTPD = /usr/local/sbin/apachectl, /etc/init.d/apache2

# User privilege specification
[user] ALL = NOPASSWD: HTTPD


Be sure that the NOPASSWD line is last in visudo, else, it risks being overwritten by later specifications.

add apache to users group

// this handy one liner is used on any new webserver..
// puts apache into the users group to allow dev teams etc..

sed -i 's/users:x:1000:admin/users:x:1000:admin,wwwrun/' /etc/group

Protect .svn directories using htaccess

// block access to .svn dirs
// should be done server-wide if you can (another snippet)

<IfModule mod_rewrite.c>
  RewriteRule ^(.*/)?\.svn/ - [F,L]
  ErrorDocument 403 "Access Forbidden"
</IfModule>

Protect .svn directories server-wide (Apache)

// protect ".svn" and "CVS" dirs (could add more)
// for server-wide protection; goes in httpd.conf
// there's a separate snippet for .htaccess-based code

<DirectoryMatch "^/.*/(\.svn|CVS)/">
  Order deny,allow
  Deny from all 
</DirectoryMatch>

Setup LAMP on Ubuntu with MyODBC support.

Go to terminal:

sudo apt-get install apache2 mysql-server php5 php5-odbc libapache2-mod-php5 php5-xsl php5-gd php-pear libapache2-mod-auth-mysql php5-mysql libmyodbc


Now hand edit (or create, if it does not exist already) /etc/odbc.ini

Here's an example odbc.ini:

[ODBC Data Sources]
odbcname     = MyODBC 3.51 Driver DSN

[odbcname]
Driver       = /usr/lib/odbc/libmyodbc.so
Description  = MyODBC 3.51 Driver DSN
SERVER       = my.database.server.com
PORT         =
USER         = USER
Password     = XXXXXX
Database     = DBNAME
OPTION       = 3
SOCKET       =

[Default]
Driver       = /usr/local/lib/libmyodbc3.so
Description  = MyODBC 3.51 Driver DSN
SERVER       = localhost
PORT         =
USER         = root
Password     =
Database     = test
OPTION       = 3
SOCKET       =


Remember to start and stop mysql and apache:

sudo /etc/init.d/mysql start
sudo /etc/init.d/apache2 start

sudo /etc/init.d/mysql stop
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/mysql restart
sudo /etc/init.d/apache2 restart

Run Apache in 32bit mode on Leopard

There is currently an issue resulting from the fact that Apache on Mac OS X 10.5 is built as a 64 bit application. On Macs that support 64 bits (all current Mac models), Apache will run in 64 bit mode. This is incompatible with most ODBC drivers, including those currently available from Actual Technologies.
Until we can make 64 bit versions of our drivers available, we recommend a workaround to force Apache to run in 32 bit mode. Using the Terminal prompt, enter the following commands (this is for ppc machines, for intel switch ppc7400 to i386):
sudo mv /usr/sbin/httpd /usr/sbin/httpd.ub

sudo lipo -thin ppc7400 /usr/sbin/httpd.ub -output /usr/sbin/httpd.ppc7400

sudo ln -s /usr/sbin/httpd.ppc7400 /usr/sbin/httpd 

Now restart Web Sharing in the System Preferences, and continue with the instructions for "Invoking ODBC Functions from ODBC" below.
NOTE: if you ever need to restore Apache to 64 bit mode, just enter the following command:
sudo mv /usr/sbin/httpd.ub /usr/sbin/httpd

Apache rewrite rules

Pass all requests for non-existing files or directories to index.php
RewriteCond        %{REQUEST_FILENAME}        !-f                
RewriteCond        %{REQUEST_FILENAME}        !-d
RewriteRule        ^(.*)$                    index.php        [L]