Thursday, March 7, 2013

How to force ssh sessions into screen


I can't count how many times I've been working in a terminal session to a remote host and have had the connection drop.  Of course it usually happens when I'm editing a file in Vi or right towards the end of a big download or during a long command causing me have to start over or recover the file.  How do you keep this from happening?  Screen.

Screen is a helpful little script that allows you to connect and disconnect from a machine without losing your place.  It also allows you to do things like open multiple terminal sessions in the same window, share your terminal with other users, and split your window view into multiple terminal sessions.  I use screen regularly, but sometimes forget to start or connect to the session.  Here's how to force your terminal sessions into screen.

We'll start by creating a .bin folder in your user directory (to keep it from getting cluttered)
mkdir ~/.bin

Next we'll add this .bin directory to our path (I know .bash_profile already adds a bin folder, but I want my $HOME directory clean and uncluttered). 
 printf "if [[ -d ~/.bin ]]; then\n\tPATH=$PATH:~/.bin;\nfi" >> ~/.bashrc


Be sure that .bashrc is sourced from your .bash_profile by looking for the following lines in ~/.bash_profile
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

Next we'll create a screen-check script that will actually do the checking and force the session to use screen.  Put the following lines in ~/.bin/screen-check :


#!/bin/bash
# Check if already in a screen and force session into a screen
if [[ $TERMCAP == *screen* ]]; then
        echo "Already in screen"
else
        screen -dRR myscreen
fi

Then simply add the screen-check script to your .bashrc file and source it 
chmod 755 ~/.bin/screen-check
echo "screen-check" >> ~/.bashrc
source ~/.bashrc

You should now be in a screen session, and every time you open a terminal to the machine it should force you into that session.  Hope that saves someone the headache of having to redo work that got cut off due to a connection issue.

Learn more about screen.

How to install laravel, apache, php and mysql on CentOS


While Laravel is pretty easy to install, I figure it couldn’t hurt to have a step-by-step guide that you could copy and paste into a new CentOS6 server.  (This should work on CentOS5+ and Red Hat as well)
This how-to is available as a Google Doc as well.








# Install epel and ius repositories 
rpm -ivh ius-release-1.0-10.ius.el6.noarch.rpm;
rpm -ivh epel-release-6-5.noarch.rpm;
yum clean all; yum update; yum upgrade;  

# Install apache, php, mysql, git and a few other goodies  
yum install -y httpd php54 php54-cli php54-common php54-gd php54-ldap php54-mbstring php54-mcrypt php54-mysql php54-odbc php54-pdo php54-pear php54-pecl-geoip php54-pecl-mongo php54-pecl-memcache php54-pgsql php54-soap php54-xml php54-xmlrpc mysql mysql-server screen git bind-utils;
service httpd restart; 

# Replace vi with vim (optional)  
mv /bin/vi /bin/vi.bak; 
ln -s /usr/bin/vim /bin/vi;
echo "export EDITOR=/usr/bin/vim" >> ~/.bashrc;
source ~/.bashrc; 

# Install Composer  
curl -k -sS https://getcomposer.org/installer | php;
sudo mv composer.phar /usr/local/bin/composer; 

# Grab Laravel and get it set up  
#unzip develop; 
#mv laravel-develop /var/www/example.com; 
#rm -f develop;
git clone git://github.com/laravel/laravel.git -o laravel -b develop /var/www/example.com
cd /var/www/example.com;
composer install;
chmod -R 2777 /var/www/example.com/app/storage; 

# Set up virtual host in apache  
printf "<VirtualHost *:80>\n\tServerName example.com\n\tServerAlias www.example.com\n\tDocumentRoot /var/www/example.com/public\n\tCustomLog /var/log/httpd/example.com-access.log combined\n\n\t<Directory \"/var/www/example.com\">\n\t\tAllowOverride All\n\t</Directory>\n</VirtualHost>" >> /etc/httpd/conf.d/example.com.conf;
service httpd graceful;
That should about do it.  After you’ve set up your dns to point example.com to your server’s ip address, simply visit your site in a browser and you should get the “Hello World!” Laravel start page.