Posts tagged Linux

Linux – Nginx – Config – Concate $var to string

0

Yesterday I ran into a situation where I had to concatenate a variable to a string within an NGinx config file. Seemed easy enough, but found it to be quite troublesome. At first I thought there may have been a special concat character, like the ‘.’ in PHP, but there isn’t, rather the concatenation construct for NGinx is similar to BASH. Check out the following example:

set $foo = 'foo';

set $foobar "${foo}_bar"; #Concatenating $foo to _bar

Linux – CentOS – No acceptable C compiler found in $PATH

1

If you attempt to compile an application from source and run into a ‘no acceptable C compiler found in $PATH’ on a CentOS system, just run:

yum install gcc

Linux – ACK – Basic Usage

0

There are times where you may need to search for code snippets throughout your codebase, for example if you are planning on deprecating a function, or change the argument list etc. I used to use ‘fgrep’, but a friend locked me onto ‘ack’, which I find much more useful out of the two. You can find more information at ack’s website @ http://betterthangrep.com/.

By default ack searches are recursive, so it’s one less flag you have to pass (vs fgrep -r), not that big of a deal, but still nice. Also ack output is easier to read and can be configured to your preferences.

Basic Search

ack 'string'

Case Insensitive Search

ack -i 'string'

Exclude Dir

There may be times where you want to exclude a dir from the search, so you don’t end up a ton of permission errors for example.

ack --ignore-dir=dir 'string'

Linux – Apache – Php – php_network_getaddresses – Name or service not known

0

Came across an issue where I was getting “SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known” errors when one of my applications was trying to establish a connection to a MySQL db.

In my /etc/resolv.conf file I didn’t have the domain with the host DNS information, so I added it. At this point I was able to ping the host via command line, but was still getting the errors. After some Googling I came across http://albertech.net/2011/05/fix-php_network_getaddresses-getaddrinfo-failed-name-or-service-not-known/ where the author indicated that if you can ping the host via command line but are still getting the errors, try restarting Apache. So I followed his suggestion, restarted Apache, and it worked like a charm.

Linux – CentOS – Unable to Set Timezone

2

This morning I had a weird issue on one of my production boxes. I did a ‘date’ and noticed that the timezone was EDT, when it should be UTC. So I changed the timezone to UTC by:

rm -f /etc/localtime

ln -sf /usr/share/zoneinfo/UTC /etc/localtime

Now when I did ‘date’, I still saw the timezone was EDT, when it should have been UTC. I was directed by the hosting company to reinstall the ‘tzdata’ package, so I did, and then redid the steps to change timezone:

yum reinstall tzdata

rm -f /etc/localtime

ln -sf /usr/share/zoneinfo/UTC /etc/localtime

Now, when I do ‘date’, it shows the correct date/time.

Just wanted to put it out there in case anyone else has the same issue.

Linux – CentOS 64 – Ruby, RubyGems, Capistrano

4

Intro

After spending about 1 1/2 years building my prototype website I am finally ready to push my code to a demo server to help with potential client presentations. Right now, my project is hosted on github.com and they recommended an application known as ‘Capistrano’ to deploy code to remote servers. This article will outline the numerous roadblocks I had to go through, and try to piece various solutions into one location so other devs who are wanting to use Capistrano to deploy code will not have to navigate the same trials and tribulations that I did.

Before I start into how to setup Ruby, RubyGems, and Capistrano on CentOS, I’d like to define each one, so you have a clear understanding of why you need them to accomplish the final goal.

– Update 12.7.2011 –
If you are running into a “it seems your ruby installation is missing psych” message when running ruby, check out my post on how to resolve the issue: http://melikedev.com/2011/12/07/linux-centos6-ruby-it-seems-your-ruby-installation-is-missing-psych/

Ruby

http://www.ruby-lang.org/en/

As defined by the official Ruby website; “A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.”

Why do we need Ruby? Because Capistrano is written in Ruby.

RubyGems

http://rubygems.org/

RubyGems is a repo for Ruby Apps, much like Yum for CentOs. Even though we could download source and compile outside of RubyGems, I preferred to go through RubyGems to ensure a higher probability of system stability.

Capistrano

http://rubygems.org/gems/capistrano (source via rubygems)

https://github.com/capistrano/capistrano (source via github.com)

As defined by Capistrano website; “Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH”

Basically, Capistrano will execute a series of commands on remote machines using a highly configurable config file.

Can’t use YUM for Ruby

So this is where I spent a lot of time trying Capistrano to work on my system. I wanted to use Yum to manage the Ruby install, but as you probably know, CentOs is very stable, and this stability derives from the fact that applications spend years in a specific version, and Ruby is no exception. If you use yum to install Ruby, you will find that it is current up to version 1.8.5, however we need version 1.8.6+ (for RubyGems to work correctly).

So I tried to use a “ruby” repo as outlined in various postings scattered throughout Google. In essence they each said you can create a “Ruby” repo using the following config:

vi /etc/yum.repos.d/ruby.repo
1
name=ruby
baseurl=http://repo.premiumhelp.eu/ruby/
gpgcheck=0
enabled=0

Now, in theory, you can type the following command:

yum --enablerepo=ruby list *RUBY*

I experienced a lot of frustration with this part because it kept listing out the default version (1.8.5) and I couldn’t figure out why. After a few hours and some tinkering I realized that in my /etc/yum.conf file I had set my excludes to exclude everything except 64 bit applications. Guess what? Ruby via the “Ruby” repo is not a 64 bit application so it wasn’t showing up. So I tried the following:

yum list *RUBY* --enablerepo=ruby --disableexcludes=all

Sure enough I finally saw Ruby version 1.8.6 listed, so I told Yum to install it and ran into yet another roadblock. The i686 version of Ruby wanted to install another 44 packages (dependencies) that were also i686. No way did I want to bork my stable system with 2 versions of common dependencies or reduce performance by downgrading from a 64 bit app to a 32 bit app. At the end of the day I was left with only one choice; download source and compile.

Compile and Install Ruby from Source

Simple enough:


cd /usr/local/source

wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.gz

tar -xzvf ruby-1.8.7-p174.tar.gz

cd ruby-1.8.7-p174

./configure

make

make install

ruby --version

Now, if installed correctly ruby should return: “ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]“. Sweet, now we can move onto RubyGems.

Install RubyGems

Installing rubygems is easier than having to compile from source because now that you have Ruby installed you can use it to do the install for you:


cd /usr/local/src

wget http://production.cf.rubygems.org/rubygems/rubygems-1.4.1.tgz

tar -xzvf rubygems-1.4.1.tgz

cd rubygems-1.4.1

ruby setup.rb

RubyGems should now be installed, now onto Capistrano.

Install Capistrano

Installing Capistrano is even easier than installing Ruby or RubyGems:

gem install capistrano

After install completes you can go to your html source and type:

 capify .

Which will create a config file (config/deploy.rb).

Configure Capistrano

Configuring your deploy config will be covered in another article soon to come.

Closing

Now you have Ruby, RubyGems, and Capistrano installed on your CentOS server and after some configuration setup will be able to start pushing your code to remote servers.

Hope this article helped.

Linux – Install Memcached on CentOS

0

According to http://memcached.org/, Memcached is a “distributed memory object caching system”. What this means is that memcached will return stored values from memory rather than other storage mediums such as disk or database. How do you access these values? Well, depending upon the key construct you used, you just need to provide the memcached with the proper key and you will gain access to stored values (also known as a key-value pair).

Memcached provides a layer which sits between your application and your backend storage solution. What memcached does is allow you, the developer, to store commonly retrieved data into the cache layer so subsequent requests will use the cache version rather than querying your database or pinging your filesystem. As you can probably tell, this is a much faster solution and reduces the resources needed by your databases.

There is a lot of power that memcached provides, but as a developer you need to ensure your app can correctly harness this power. What I mean by this is; imagine you update a value in your database, then through your application you submit a form to confirm the value has indeed been changed, but for some reason you keep seeing the old value. Guess what? Your old value was cached and you optimized your application to use cache before hitting the database. As a developer you need to create a method of clearing cache when needed so you don’t bang your head on the desk trying to figure out why values you changed are not being respected by your application.

Now that you have a basic understanding of what memcached does, lets install it on your centos system.

Memcached is not available to your system by default (via YUM), you will have to gain access to rpmforge (a collection of rpms). If you don’t have rpmforge setup, follow this guide: http://melikedev.com/2010/03/10/centos-basic-usage-guide/ in the section RPMForge.

Now that you have rpmforge setup you should be able to get access to memcached. Try:

yum list memcached

If all looks good, then go ahead and install:

yum install memcached

It should also install about 10 more dependencies all from rpmforge, but shouldn’t have any negative impact on your system. When that’s finished you now have memcached installed on your system.

Now it’s time to configure. First we want to add a user to the system so we can run the memcached(aemon) as a non-root user:

adduser memcached

Now we can edit memcached settings:


# /etc/sysconfig/memcached

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

Now you should be able to start your memcached server:

sudo -u memcached /etc/init.d/memcached start

If you get an permission related error regarding the lock file, you will have to ensure that the memcached user can write the log file to /var/lock/subsys (This lock file ensures you don’t run multiple memcacheds at the sametime). What I did is:


chown root:daemon /var/lock/subsys #allow daemon group, group control over subsys

chmod 775 /var/lock/subsys #make subsys dir writable by group owner

You can now test your memcached server by following these steps outlined here: http://melikedev.com/category/memcache/.

Now that you have an up and running cache server you should develop a caching class within your app that will access your memcached server so you can save commonly run query results within the caching layer. Don’t get carried away though, try to be strategic about what you are populating your cache with, and don’t forget to develop some mechanism for clearing cache when needed.

Good luck.

CentOS – Basic Usage Guide

1

This post will house basic usage commands for CentOS 5.4. It is a ‘living doc’, meaning that it will be updated over time so bookmark if interested.

YUM

Yum is the package manager built into CentOS distros and by package manager I don’t mean your local delivery guy, I mean how you can install, update, and remove software packages from your system.  Listed here are YUM commands you may find useful.

Update package repo:

yum update

View installed packages:

rpm -qa
#OR
yum list installed

View available updates:

yum list updates

Remove existing i386 apps from x64 system (USE WITH CAUTION!)


yum list installed | fgrep i386 > tmp.txt

yum remove $(awk '{print $1}' tmp.txt | xargs)

To only install 64 bit apps, add following line to /etc/yum.conf:

exclude=*.i386 *.i586 *.i686

Install yum-utils. (Useful set of utilities including package-cleanup)

su -c 'yum install yum-utils'

If you ever get a “missing dependency” error when trying to update a package via yum, check to make sure you have the right version installed (i686 vs x86_64). I had this issue when trying to update glibc-common. It wouldn’t update because the version of glibc was i686, which I explicitly removed via the yum.conf file (explained above).

yum list <package name>

Remove a package:

yum remove <package name>

Check available updates:

 yum check-update

Misc

Check CentOS version:

cat /etc/redhat-release

Reboot:

reboot

Delete User (-r will remove home dir):

userdel -r <username>

If user is logged in, force log them out:

pkill -KILL -u <username>

Change timezone:

Note: Personally, I highly recommend you set your system's local timezone to UTC. This will avoid any issues where some logs are in UTC, some are in local time, and will ensure a consistent time construct across all apps and modules such as php and mysql. Not to mention if you happen to move the server to a different timezone...


sudo rm /etc/localtime

sudo ln -s /usr/share/zoneinfo/UTC /etc/localtime

RPMForge

If you need to install apps like irssi you will have to use rpmforge repo. To install follow the instructions @ http://wiki.centos.org/AdditionalResources/Repositories/RPMForge

To keep your system as pristine as possible, I would edit the /etc/yum.repos.d/rpmforge.repo file and change enabled=1 to enabled=0. This will ensure you are not overriding centos approved packages with newer rpmforge packages. To install a package that resided in rpmforge only, do the following:

yum install <some_package> --enablerepo=rpmforge

This will allow you to use rpmforge on a case-by-case basis giving you much more control over what gets onto your system.

IRSSI

In order to get irssi to work on centos you may have to create a symlink:


cd /usr/lib64

ln -s perl5/5.8.8/x86_64-linux-thread-multi/CORE/libperl.so

IPTables

If you are running httpd and can't connect, try disabling IPTables:

/etc/init.d/iptables stop

Linux – Basic TOP Usage

0

Top is a Linux app that will display currently running tasks (aka processes). The benefit of top to a developer comes in the form of profiling applications. If you have been developing apps for any amount of time you have probably done something where the application becomes unresponsive and you don’t know why. Chances are the application is eating cpu and memory resources and not readily responding. Below are basic top usages that you may find helpful.

To start top, type the following:

top

Pressing any of the following keys while running top will result in the explained action.

q

Press q to exit top.

u

Press u to enter specific username and view only processes belonging to them.

s

Press s to change the number of seconds between refreshes (default is 3).

c

Perhaps one of the most helpful commands. Press c to view the process’s full command line entry. For example, php becomes php /home/user/someScript.php

1

Press 1 to view ALL cpus (or cpu cores). Extremely useful when profiling applications.

There are plenty more of options available but these seem to be the most commonly used. To view more options:

man top

Linux – TAR (Tape Archive)

0

TAR, in *nix context, is an acronym for Tape Archive. Its a command that allows you to compress entire directories (or files) into one file which can then be compressed even further by passing a compression flag.  Note; there are several other flags which can be used with the TAR command, however the following are commonly used and briefly discussed.

Create TAR

In create context, flag meanings are:

  1. c – Create  new archive
  2. j or z -
    1. j – Compress the resulting tar file using bzip
    2. z – Compress the resulting tar file uzing gzip
  3. v – Verbose
  4. f – User will set resulting tar filename, if not passed it, TAR may use env settings set in /etc/default/tar

Create TAR File From Directory

tar -cvvf someFolder.tar someFolder/

Create TAR File From File

tar -cvvf someFile.tar someFile.log

Create TAR File From Folder and Compress Using bzip

 tar -cjvf someFolder.tar.bz someFolder/

Create TAR File from Folder and Compress using gzip

 tar -czvf someFolder.tar.gz someFolder/

Extract TAR

In extract context, flag meanings are:

  1. x – Extract a tar file
  2. j or z -
    1. j – Un-compress the resulting tar file using bzip
    2. z – Un-compress the resulting tar file using gzip
  3. v – Verbose (same as create)
  4. f – Name of tar file being extracted (same as create)

Extracting TAR File (uncompressed)

tar -xvvf someFolder.tar

Extracting TAR File (compressed w/ gzip)

tar -xzvf someFolder.tar.gz

Extracting TAR File (compressed w/ bzip2)

 tar -xjvf someFolder.tar.gz
Go to Top