tag:blogger.com,1999:blog-377933192024-02-19T05:27:12.693-05:00Practical Web ProgrammingJoel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.comBlogger285125tag:blogger.com,1999:blog-37793319.post-18782032025896635662017-12-09T08:38:00.000-05:002017-12-09T08:38:00.213-05:00Turn Off/On Ubuntu Server at Specified TimeIf you are running a headless ubuntu server as a media server that is on 24/7, you could be wasting several hours of energy - especially at night when no one is using it. To solve this problem, you can automatically shut it down and turn it on at certain time using <code>rtcwake</code>. See below for step by step:
<ol>
<li>Login as root and open `crontab`.</li>
<pre class="sourcecodes">
$ sudo su
$ crontab -e
</pre>
<li>Add the command to <code>crontab</code>.</li>
<pre class="sourcecodes">
# Shutdown at 12 AM and awake afer 8 hours (8:00 AM)
0 0 * * * sudo /usr/sbin/rtcwake -m disk -s 28800 > /var/log/crontab.log
</pre>
</ol>Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-26007012703351363082016-12-23T22:33:00.000-05:002017-02-05T00:08:51.990-05:00How to Install and Configure Samba in Ubuntu 16.04<ol>
<li>
Install Samba
<pre class="sourcecodes"> sudo apt-get update
sudo apt-get install samba
</pre>
</li>
<li>
Set a password for your user in Samba
<pre class="sourcecodes"> sudo smbpasswd -a <user_name>
</pre>
Note: Samba uses a separate set of passwords than the standard Linux system accounts (stored in /etc/samba/smbpasswd). Having said that, I always use here whatever username I use for my Linux account.
</li>
<li>
Create a directory to be shared
<pre class="sourcecodes">
mkdir /home/<user_name>/<folder_name>
</pre>
</li>
<li>
Edit the file "/etc/samba/smb.conf"
<pre class="sourcecodes">
sudo vim /etc/samba/smb.conf
</pre>
And add the following:
<pre class="sourcecodes">
[global]
workgroup = HOME_NETWORK
#(Set this to the name you want the shared folder to have)
[share]
comment = File server share folder
path = /home/<user_name>/share/
browsable = yes
read only = no
guest ok = yes
writable = yes
valid users = <samba_user_name>
create mask = 0755
</pre>
</li>
<li>
If you setup a firewall, allow port 445
<pre class="sourcecodes"> ufw allow 445
</pre>
</li>
<li>
Restart the samba:
<pre class="sourcecodes"> sudo service smbd restart
</pre>
</li>
<li>
Once Samba has restarted, use this command to check your smb.conf for any syntax errors
<pre class="sourcecodes"> testparm
</pre>
</li>
</ol>
<div style="border-left: 2px solid #fff; margin-left: 20px; padding-left: 10px;">
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-34563967110672685672016-08-07T12:57:00.000-04:002016-08-07T12:58:14.313-04:00Install Latest MySQL in Ubuntu ServerThis assumes that you already have a running Ubuntu server with Apache installed. To install basic security in Ubuntu, see <a href="http://kabalweg.blogspot.com/2016/01/basic-security-installs-for-ubuntu.html">Basic Security Installs for Ubuntu</a>. This instructions was extracted from <a href="https://www.phusionpassenger.com/library/install/apache/install/oss/trusty/" rel="nofollow">here</a>.
<br /><br />
1. Update the package index on your server
<br />
<pre class="sourcecodes">sudo apt-get update
</pre>
<br />
2. Install the package
<br />
<pre class="sourcecodes">sudo apt-get install mysql-server
</pre>
<br />
3. Run the included security script. This changes some of the less secure default options for things like remote root logins and sample users.
<br />
<pre class="sourcecodes">sudo mysql_secure_installation
</pre>
<br />
4. If you're using a version of MySQL earlier than 5.7.6, you should initialize the data directory by running command below.
<br />
<pre class="sourcecodes">sudo mysql_install_db
</pre>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-77464829197131188552016-07-10T11:41:00.001-04:002016-07-10T11:41:07.494-04:00How to Debug Rails Application Using Pry Remote<div dir="ltr">1. Add pry remote in gemfile<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span style="color:rgb(56,118,29)">gem 'pry-remote', '~> 0.1.8'</span> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><font color="#38761d">gem 'pry-nav', '~> 0.2.4'</font> </blockquote><div><br></div><div><br></div><div>2. Run <font color="#38761d">bundle install</font> in the terminal </div><div><br></div><div><br></div><div>3. Add the following line in application.rb:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><font color="#38761d">require 'pry-remote'</font></blockquote><div> </div><div> </div><div>4. Add the following in the code where you want to stop executing and debug:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><font color="#38761d">binding.remote_pry</font></blockquote><div><br></div><div><br></div><div> 5. Load the application in the browser. The application will stop execution once it encounters the <font color="#38761d">binding.remote_pry</font> line</div><div><br></div><div><br></div><div>6. Open up a new terminal and run the following code:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><font color="#38761d">pry-remote</font></blockquote><div><br></div><div><br></div><div>7. To navigate in the terminal, use the following command:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><font color="#38761d">step<br>next<br>continue<br>exit</font></blockquote><div> </div></div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-70076984261604974542016-01-09T07:30:00.000-05:002016-06-08T22:45:50.911-04:00Deploying Rails Application for the First TimeI always forget these steps when deploying a Rails app for the first time. So I'm finally writing this in as a guide for the future.
<br />
<br />
This assumes that you already have a running Ubuntu server with Apache and Passenger installed (see <a href="http://kabalweg.blogspot.com/2016/01/basic-security-installs-for-ubuntu.html">Basic Security Installs for Ubuntu</a> and <a href="http://kabalweg.blogspot.com/2016/01/how-to-install-passenger-apache-in.html">How to Install Passenger + Apache in Ubuntu</a>).
<br />
<ol>
<li>Clone the git repo.
<pre class="sourcecodes">git clone git@bitbucket.org:kabalweg/app_name.git
cd app_name
bundle install
</pre>
If you get a "bundle not yet installed" error, run the following comment:
<pre class="sourcecodes">
gem install bundle
</pre>
</li>
<li>Go to /var/www/ type and this command to create a symbolic link.
<pre class="sourcecodes">sudo ln -s /home/kabalweg/app_name/ ./app_name
</pre>
</li>
<li>Go to /etc/apache2/sites-available and create a virtualhost file, app_name.conf, or copy and existing one and edit the appropriate values in that file and save.
<pre class="sourcecodes"><VirtualHost *:80>
ServerName app_name.net
Redirect permanent / http://www.app_name.net/
</VirtualHost>
<VirtualHost *:80>
ServerName www.app_name.net
ServerAdmin kabalweg@gmail.com
# Set the environment to production
RailsEnv production
<IfModule mod_passenger.c>
# Set to on when debugging errors
PassengerFriendlyErrorPages off
#PassengerRoot /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini
#PassengerDefaultRuby /home/kabalweg/.rvm/gems/ruby-2.2.1@rails4.2/wrappers/ruby
PassengerMaxPoolSize 2
PassengerPoolIdleTime 0
PassengerMaxRequests 1000
</IfModule>
DocumentRoot /var/www/app_name/public
<Directory /var/www/app_name/public>
AllowOverride all
Options -MultiViews
#Require all granted
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</pre>
</li>
<li>Disable the default virtualhost and enable the new virtualhost file and activate it.
<pre class="sourcecodes">sudo a2dissite 000-default.conf
sudo a2ensite app_name.conf
sudo apachectl -k graceful
</pre>
</li>
<li>Generate secret key and put the resulting key in the secret.yml file.
<pre class="sourcecodes">rake secret RAILS_ENV=production
</pre>
Note: It's not a good and safe practice to put production config values in the repo.
A good way is to ignore config files (*.yml) using git so it don't get save in the repo,
then just manually create this files, with the correct values, in the production server.
</li>
<li>Pre-compile assets.
<pre class="sourcecodes">rake assets:precompile RAILS_ENV=production
</pre>
</li>
<li>Restart application by typing below in your application's root directory.
<pre class="sourcecodes">touch tmp/restart.txt
</pre>
Create this folder (tmp) if you don't have this.
</li>
</ol>Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-51416378796265064132016-01-09T06:53:00.001-05:002016-12-17T00:42:37.843-05:00How to Install Passenger + Apache in Ubuntu 16.04 LTSNote: This assumes that you already have a running Ubuntu server. To install basic security in Ubuntu, see <a href="http://kabalweg.blogspot.com/2016/01/basic-security-installs-for-ubuntu.html">Basic Security Installs for Ubuntu</a>. This instructions was extracted from <a href="https://www.phusionpassenger.com/library/install/apache/install/oss/trusty/" rel="nofollow">here</a>.
<ol>
<li>Install Apache Server
<pre class="sourcecodes">
sudo apt-get update
sudo apt-get install apache2
</pre>
</li>
<li>Set Global ServerName to Suppress Syntax Warnings: "AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message"
<pre class="sourcecodes">
sudo vim /etc/apache2/apache2.conf
</pre>
and enter at the end of the file:
<pre class="sourcecodes">
ServerName server_domain_or_IP
</pre>
</li>
<li>Test config so far
<pre class="sourcecodes">
sudo apache2ctl configtest
</pre>
</li>
<li>Restart Apache for the changes to take effect
<pre class="sourcecodes">
sudo systemctl restart apache2
</pre>
</li>
<li>Install Passenger packages.
<pre class="sourcecodes">
# Install our PGP key and add HTTPS support for APT
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
# Add our APT repository
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
# Install Passenger + Apache module
sudo apt-get install -y libapache2-mod-passenger
</pre>
These commands will install Passenger + Apache module through Phusion's APT repository.
</li>
<li>Enable the Passenger Apache module and restart Apache
<pre class="sourcecodes">
sudo a2enmod passenger
sudo apache2ctl restart
</pre>
</li>
<li>Check installation
<pre class="sourcecodes">
sudo passenger-config validate-install
</pre>
All checks should pass. If any of the checks do not pass, please follow the suggestions on screen.
</li>
<li>Check whether Apache has started the Passenger core processes by running the following:
<pre class="sourcecodes">
sudo passenger-memory-stats
</pre>
</li>
</ol>Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-51360329400578252562016-01-08T22:17:00.000-05:002016-01-09T06:53:48.140-05:00Creating a Skeleton Rails Application without Database and Test Unit<ol>
<li>1. Create application without database access and test unit
<pre class="sourcecodes">
$ rails new [app_name] --skip-active-record --skip-test-unit
</pre>
</li>
<li>Include Bootstrap
<ul>
<li>Download Bootstrap and extract content</li>
<li>Copy bootstrap.min.css and bootstrap.css to /app/assets/css folder</li>
<li>Copy bootstrap.min.js and bootstrap.js to /app/assets/js folder</li>
<li>Copy fonts folder to /app/assets folder</li>
</ul>
Note: The reason why I put bootstrap.css in the main root of asset folder is so I can create a
file (ex: custom.css) that will over ride the Bootstrap.css rules in case I want to.
</li>
<li>Create main controller that will serve as root file.</li>
<pre class="sourcecodes">
$ rails generate controller main_controller index --no-assets --no-helper
</pre>
<li>Set the root route to main_controller#index
<pre class="sourcecodes">
root 'main_controller#index'
</pre>
</li>
</ol>Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-75952456736952336442016-01-04T03:36:00.000-05:002018-05-04T22:50:41.838-04:00Basic Security Installs for UbuntuThe original idea of this post was taken from <a rel="nofollow" href="http://plusbryan.com/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers">My First 5 Minutes On A Server; Or, Essential Security for Linux Servers</a>. As I build my server, I follow it but some of it's recommendation does not fit my requirements (ex: connecting via SSH only on certains IPs, which locked me out on several occasions). This post is my own "concoction". This assumes that you already have a fresh server running with only root as user.
<ol>
<li> Login as root and set root password
<pre class="sourcecodes">
passwd
</pre>
It's always good to use a strong root password. I recommend <a href="http://www.randompasswordgenerator.info/">Random Password Generator</a> for this.
</li>
<li> Update Ubuntu
<pre class="sourcecodes">
apt-get update
apt-get upgrade
</pre>
</li>
<li>Install Fail2ban
<pre class="sourcecodes">
apt-get install fail2ban
</pre>
Fail2ban is a daemon that monitors login attempts to a server and blocks suspicious activity as it occurs. It’s well configured out of the box.
</li>
<li>Create user and set-up user folders
<pre class="sourcecodes">
useradd deploy
mkdir /home/deploy
mkdir /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
</pre>
</li>
<li>Change deploy user's login shell with the 'chsh' command. This will make sure that deploy user will have a more interactive shell.
<pre class="sourcecodes">
sudo chsh -s /bin/bash deploy
</pre>
</li>
<li>Require public key authentication for logging in
<pre class="sourcecodes">
vim /home/deploy/.ssh/authorized_keys
</pre>
Copy and paste the contents of the id_rsa.pub on your local machine and any other public keys that you want to have access to this server to the <code>/home/deploy/.ssh/authorized_keys</code> file. Save and close the file.
</li>
<li>Lock down authorized_keys file and change owner of user folder
<pre class="sourcecodes">
chmod 400 /home/deploy/.ssh/authorized_keys
chown deploy:deploy /home/deploy -R
</pre>
</li>
<li>Test the new user (deploy) in a new terminal window
<pre class="sourcecodes">
ssh deploy@<IP_OF_DROPLET>
</pre>
<li>While connected as <code>deploy</code>, generate SSH key. This will be used when connecting to github or bitbucket
<pre class="sourcecodes">
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
</pre>
</li>
<li>
Logout user (deploy), close the the terminal and go back to the terminal where you are logged as root.
<pre class="sourcecodes">
exit
</pre>
</li>
<li>Change deploy user password. This password will be used when doing sudo commands
<pre class="sourcecodes">
passwd deploy
</pre>
</li>
<li>Change default text editor to your preference (I like Vim)
<pre class="sourcecodes">
update-alternatives --config editor
</pre>
</li>
<li>Comment all existing user/group grant lines
<pre class="sourcecodes">
visudo
</pre>
Add the following line:
<pre class="sourcecodes">
root ALL=(ALL:ALL) ALL
deploy ALL=(ALL:ALL) ALL
</pre>
This will grant sudo access to the deploy user when they enter the proper password.
</li>
<li>Set up a firewall to further secure the server using ufw.
<pre class="sourcecodes">
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable
</pre>
</li>
<li>Enable automatic security updates
<pre class="sourcecodes">
apt-get install unattended-upgrades
vim /etc/apt/apt.conf.d/10periodic
</pre>
Update the file to look like this:
<pre class="sourcecodes">
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
</pre>
One more config file to edit:
<pre class="sourcecodes">
vim /etc/apt/apt.conf.d/50unattended-upgrades
</pre>
Update the file to look like below. This will enable security updates only:
<pre class="sourcecodes">
Unattended-Upgrade::Allowed-Origins {
"Ubuntu lucid-security";
// "Ubuntu lucid-updates";
};
</pre>
or
<pre class="sourcecodes">
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
// "${distro_id}:${distro_codename}-updates";
// "${distro_id}:${distro_codename}-proposed";
// "${distro_id}:${distro_codename}-backports";
};
</pre>
</li>
<li>Restart server and you're done!
<pre class="sourcecodes">
reboot
</pre>
</li>
</ol>Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-79765494138729307172015-06-05T21:29:00.001-04:002015-06-05T21:29:32.788-04:00Compare Between Branches in Github<div dir="ltr">
<div>
Before you deploy or when you make a release note, typically you would compare master with develop branch to get the differences and find out what has changed. In Github, it's so easy to do that. All you have to do it is go to the following link and supply the branches that you want to compare</div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;"><a href="https://github.com/kabalweg/my_app/compare/[OUTDATED_BRANCH]...[UPDATED_BRANCH]">https://github.com/kabalweg/my_app/compare/[OUTDATED_BRANCH]...[UPDATED_BRANCH]</a></span></blockquote>
<div>
<br /></div>
<div>
Example:</div>
<div>
<br /></div>
<div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;"><a href="https://github.com/kabalweg/my_app/compare/master...develop">https://github.com/kabalweg/my_app/compare/master...develop</a></span></blockquote>
<div>
<br /></div>
<div>
You can also compare between release tags, like so:</div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;"><a href="https://github.com/kabalweg/my_app/compare/[PREVIOUS_TAG]...[NEW_TAG]">https://github.com/kabalweg/my_app/compare/[PREVIOUS_TAG]...[NEW_TAG]</a></span></blockquote>
<div>
</div>
<div>
Example:</div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;"><a href="https://github.com/kabalweg/my_app/compare/2015-05-28...2015-05-29">https://github.com/kabalweg/my_app/compare/2015-05-28...2015-05-29</a></span></blockquote>
<div>
<br /></div>
<br /></div>
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-43522702594815661872015-06-05T21:29:00.000-04:002015-06-05T21:29:11.231-04:00git: Delete All Local Branches that Already Merged to Master<div dir="ltr">
<div class="">
After sometime using git and creating branches for every feature or bug, you will end up with a lot of branches. Some has been merged to the master branch and some are not. For those that been merged to master, there is no point for them to be in your local machine, so it's better to delete those so it will not clatter your local repository. To do that you just run the command below in your local repo:</div>
<div class="">
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;">git branch --merged | grep -vE "\*|\develop|\master" | xargs -n 1 git branch -d</span></blockquote>
<div class="">
<br /></div>
<div class="">
This command will delete all local branches that are merged to master except for the current checked out branch, develop and master.</div>
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-71810564157665938582015-04-15T19:30:00.000-04:002015-04-16T09:48:31.233-04:00Easily Prepare master Branch for Deployment<div dir="ltr">
<div>
<br /></div>
<div>
1) Create a bash script named "prepare_for_deployment" in the root folder of your project and put the git command below:</div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #38761d;">release_date=$(date +'%Y-%m-%d')<br /> git checkout master<br /> git fetch<br /> git reset --hard origin/master<br /> git merge origin/develop<br /> git push origin master<br /> git tag $release_date -m "$release_date"<br /> git push --tags origin $release_date</span></blockquote>
<div>
</div>
<div>
</div>
<div>
2) Make the file executable</div>
<div>
</div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;">$ chmod u+x prepare_for_deployment</span></blockquote>
<div>
</div>
<div>
3) To run the script:</div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #6aa84f;">$ ./prepare_for_deployment</span></blockquote>
<div>
</div>
<div>
<br /></div>
<div>
NOTE: You can add the path to the script in your $PATH so you don't have to type "./" before the name.</div>
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-60939351887681033682014-07-26T09:29:00.000-04:002014-08-03T00:02:54.151-04:00How Make Symbolic Link in Linux/Unix<div dir="ltr">Every now and then I find myself creating more symbolic link especially when deploying application to production, and I always forget (more like in doubt) the order of parameter when issuing a ln command. I always search for it in google and have to visit more than a couple site to find a example. So, to save time, here is it.<div> <br></div><div>To create a soft link (most common form), use this command:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ ln -s source destination</font></blockquote><div><br></div><div>Where:</div><div><br></div><div>- source = the file or directory you want to link to</div><div>- destination = the name of the link that points to the source</div> <div><br></div><div>Ex:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#38761d">$ ln -s /home/kabalweg/my_app /var/www/my_app</font></blockquote> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-42260317022412087632014-06-18T14:28:00.001-04:002016-06-06T19:16:21.967-04:00Better Way to Generate Rails Application<div dir="ltr">
<div>
<b>Generate project with RMV.</b></div>
<div>
<br /></div>
<div>
<span style="color: #38761d;">$ mkdir myapp</span> # create the project folder</div>
<div>
<span style="color: #38761d;">$ cd myapp</span></div>
<div>
<span style="color: #38761d;">$ rvm use ruby-2.1.1@myapp --ruby-version --create</span> # create a new project-specific gemset</div>
<div>
# option “—ruby-version” creates .ruby-version and .ruby-gemset files in the root directory</div>
<div>
<span style="color: #38761d;">$ gem install rails --pre</span> # installs the most recent release of Rails </div>
<div>
<span style="color: #38761d;">$ rails new . </span> # generate application in the current directory. </div>
<div>
<br /></div>
<div>
Click here is you want to <a href="http://kabalweg.blogspot.com/2016/01/creating-skeleton-rails-application.html">create an app without database and test-unit</a>
</div>
<div>
<br />
<b>If you have no RVM in your system, follow command below.</b> </div>
<div>
<br /></div>
<div>
<span style="color: #38761d;">$ \curl -L <a href="https://get.rvm.io/">https://get.rvm.io</a> | bash -s stable --ruby</span> # install rvm and ruby</div>
<div>
<br /></div>
<div>
<span style="color: #38761d;">$ rvm get stable --autolibs=enable</span> # if you have rvm already, update it</div>
<div>
<span style="color: #38761d;">$ rvm install ruby</span> # install Ruby</div>
<div>
<br /></div>
<div>
<span style="color: #38761d;">$ rvm --default use ruby-2.1.1</span> # use and set it as default</div>
<div>
<br /></div>
<div>
<span style="color: #38761d;">$ gem update --system</span> # update gem</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
From <a href="http://railsapps.github.io/installrubyonrails-mac.html">http://railsapps.github.io/installrubyonrails-mac.html</a></div>
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-52848866971393083352014-06-18T14:28:00.000-04:002014-06-18T14:28:07.252-04:00Backup/Restore MySQL Database<div dir="ltr"><div>Backup mysql database to a sql file.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ mysqldump -u root -p[root_password] [database_name] > filename.sql</font></blockquote><div><br></div><div>Restore from sql file to database.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ mysql -u root -p[root_password] [database_name] < filename.sql</font></blockquote> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-7708949491740115622014-04-15T17:06:00.001-04:002014-04-15T17:06:07.738-04:00Using scp (Secure Copy) to Copy Files from Remote Server<div dir="ltr"><div>Copy a file from a remote host to a local machine</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ scp username@example.com:/home/kabalweg/my_archive.ta.gz /home/kabalweg/local</font></blockquote><div><br></div><div>Copy a file from a local machine to a remote host</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ scp /home/kabalweg/local/my_archive.tar.gz username@example.com:/home/kabalweg</font></blockquote><div> </div> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-12459528617569472212014-04-15T17:05:00.001-04:002014-04-15T17:05:38.692-04:00Compress/Uncompress in Linux/Unix<div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">Compress the folder named sites inside the /home/kabalweg directory:</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><blockquote class="gmail_quote" style="font-family:arial,sans-serif;font-size:13px;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ tar -zcvf my_archive.tar.gz /home/kabalweg/sites</font></blockquote><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">Uncompress file into the current directory:</div> <div style="font-family:arial,sans-serif;font-size:13px"><br></div><blockquote class="gmail_quote" style="font-family:arial,sans-serif;font-size:13px;margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ tar -zxvf my_archive.tar.gz</font></blockquote><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><b>Where:</b> </div><div style="font-family:arial,sans-serif;font-size:13px"> z = compress archive using gzip program</div><div style="font-family:arial,sans-serif;font-size:13px">c = create archive </div><div style="font-family:arial,sans-serif;font-size:13px">v = verbose i.e display progress while creating archive<br> f = archive File name </div><div style="font-family:arial,sans-serif;font-size:13px">x = extract files</div> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-19807947276716507242014-04-15T10:53:00.001-04:002014-04-15T17:06:48.336-04:00Watching Log Files Using tail and less<div dir="ltr"><div>Watch the error log and filter the output to the screen (using grep).</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">tail -f /var/log/apache/error.log | grep -v Exception | grep -v 'PHP Notice'</font></blockquote><div><br></div><div>Output the error log to the screen, filter it before doing so (using grep).<br> </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#38761d">less +F /var/log/apache/error.log | grep -v Exception | grep -v 'PHP Notice'</font></blockquote> <div><br></div><div>Note:</div><div><br></div><div>-v parameter in grep is reverse look up. It mean, all text except 'Exception' and 'PHP Notice'</div><div><br></div><div><br></div><div><b>Forward and Backward Search in less</b><br> </div><div><br></div><div>/ – search for a pattern which will take you to the next occurrence. </div><div>? – search for a pattern which will take you to the previous occurrence. </div><div>n – for next match in backward direction </div> <div>N – for previous match in forward direction<br></div><div>Shift F - similar to tail -f</div> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-12784320606152871812014-04-15T10:53:00.000-04:002014-04-15T10:53:08.651-04:00How to Create User in Mysql<div dir="ltr"><div>Create the user that can access mysql via localhost:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';</font></blockquote><div><br></div><div><br></div><div>If you want the user to access the database from anywhere, then use this:</div> <div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#38761d">CREATE USER 'username'@'%' IDENTIFIED BY 'password';</font></blockquote> <div><br></div><div><br></div><div>Give the new user permission to access all databases and all its tables:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">GRANT ALL PRIVILEGES ON * . * TO 'username'@'localhost';</font></blockquote><div><br></div><div><br></div><div>Reload the privileges:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">FLUSH PRIVILEGES;</font></blockquote> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-13765839197909869672014-03-25T11:24:00.001-04:002014-03-25T11:24:53.101-04:00Git: Merge Develop Branch to Master<div dir="ltr"><div>Normally when developing, you would make your changes in develop branch and when releasing, merge develop to master branch. Here's the best way to do it:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ git checkout master</font> # make sure you are in master branch<br><font color="#38761d">$ git fetch</font> # download commit from remote branch <span class="" style="white-space:pre"> <br> </span><font color="#38761d">$ git reset --hard origin/master</font> # removes changes/commits in local master branch (if any)<br><font color="#38761d">$ git merge origin/develop</font> # merge remote develop to master (the current branch)<br> <font color="#38761d">$ git push origin master</font> # push the changes to remote repo</blockquote> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-52554172385948787032014-03-25T11:24:00.000-04:002014-03-25T11:24:40.352-04:00Git: Delete All Local Branches That Were Already Merged to Master<div dir="ltr"><div>In Git, to clean up your local repository of already merged branches to master (aka pruning), run this command:<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$ git checkout master<br>$ git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d</font></blockquote><div><br></div><div>This is three commands in one, made possible via pipes ("|").</div> <div><br></div><div>1) git branch --merged master # list all the branches that have been merged to master</div><div>2) grep -v "\* master" # select all the branches that does not match "* master"</div> <div>3) xargs -n 1 git branch -d # execute git branch -d with 1 parameter - the branches that was returned by #2</div> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-39396957620923871432014-03-05T16:12:00.000-05:002014-03-05T16:23:28.628-05:00Fix Repeating Content in Email using Handlebars Template<div dir="ltr"><div>Recently, I reformat our email template and ran into an issue wherein the middle part of the email body is re-populating at the end of the body resulting in duplicate content. The email template was created using Handlebars. My suspect is the character encoding of the production mysql database. But what ever I do (copy the prod DB locally with same encoding, etc), I could not replicate it in my local machine.</div> <div><br></div><div>Removing the non-printable characters before rendering the content with template fixed it. Here's how I did it.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">$cleaned_text = preg_replace('/[\x00-\x1F\x80-\xFF]/', ' ', $text_with_non_printable_chars);</font></blockquote><div><br></div><div>/[\x00-\x1F\x80-\xFF]/ are characters from 0-31, 128-255 in ASCII character code.</div> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-903568951698309342014-02-18T17:04:00.001-05:002014-02-18T17:04:15.710-05:00How to enable remote access in a MySQL server<div dir="ltr"><div>Let say you are developing locally and wanted to connect to a remote mysql server for testing, if that remote server is not configured to allow access from a different machine, you won't be able to connect to it. Assuming you want to allow '<font color="#38761d">the_user</font>' user to connect remotely, here's to do it. <br> </div> <div><br></div><div>Edit your my.conf file (usually located at /etc/mysql/ folder), and change the bind-address value </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">from: bind-address = 127.0.0.1<br>to: bind-address = 0.0.0.0</font></blockquote><div><br></div><div>Login as a root user to MySQL. If you don't have the password run this command:</div><div><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#38761d">sudo mysql --defaults-extra-file=/etc/mysql/debian.cnf</font></blockquote> <div><br></div><div>Once you are logged in, run these commands</div><div><br></div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#38761d">USE mysql;<br>UPDATE user SET host='%' WHERE User='the_user';<br> UPDATE db SET Host='%' WHERE User='the_user';<br>FLUSH PRIVILEGES;</font></blockquote> <div><br></div><div>Then, restart mysql</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <font color="#38761d">sudo service mysql restart</font></blockquote> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-50853239028410359232013-12-19T17:06:00.000-05:002013-12-19T17:06:32.638-05:00git: Merge master to feature_branch Often to Minimize Conflicts<div dir="ltr">
One of the best way to avoid conflicts (or large unmanageable conflicts) is to merge master often to develop or whatever feature branch you are working on. This way, you will integrate all the changes made to master (by other developers or you) to the current branch as you progress. Here's an easy way to do it: <br />
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #38761d;">$ git checkout feature_branch<br /> $ git pull origin master</span></blockquote>
<div>
<br /></div>
<div>
The first command makes sure you are on the right branch, and the second one fetches the changes in origin/master repo and merge into the current branch. Take note that if you omit origin master from the second command, it will try to fetch and merge origin/feature_branch - not what you want to do here.</div>
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-82475297922580728332013-11-18T20:11:00.000-05:002013-11-18T20:11:52.445-05:00How to Connect to a Server via SSH<div dir="ltr"><div><br></div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font color="#38761d">ssh -p 800 <a href="mailto:user@kabalweg.com">user@kabalweg.com</a></font></blockquote> <div><br></div><div>where:</div><div><a href="http://kabalweg.com">kabalweg.com</a> = is the server</div><div>user = the user defined in that server</div><div>800 = connection port to that server</div> </div> Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0tag:blogger.com,1999:blog-37793319.post-19277361556477745392013-11-14T09:39:00.000-05:002013-11-18T20:12:03.355-05:00How to scp (Secure Copy) File from a Server to Local Machine<div dir="ltr">
The console command below will copy the file from the remote server to the desktop using the same filename.<br />
<div>
<div>
<br /></div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
<span style="color: #38761d;">scp root@remote_server:/var/lib/backup/server_backup.gz ~/Desktop</span></blockquote>
<div>
<br /></div>
<div>
<br /></div>
<div>
where: </div>
<div>
root = the user to that server</div>
<div>
remote_server = host entry in you ~/.ssh/config file</div>
</div>
</div>
Joel Badinashttp://www.blogger.com/profile/09980005746056415488noreply@blogger.com0