Host file updater

Background:

If you are a developer managing multiple virtual hosts on your system you probably have to update the /etc/hosts file quite a lot. This script gives a easy to use way to add and remove domain from the etc file.

Code:

Create a new file named “hostupdater.sh” then paste and save the code.

#!/bin/sh
# This script was developed by faisal ahmed and is provided as is without any warranty.

if grep "$2$" /etc/hosts; then
  if [ "$1" = "del" ]; then
    sed -i "/.*$2$/d" /etc/hosts
  else
    sed -i "s/.*$2$/$1 $2/g" /etc/hosts
  fi
else
  echo $1 $2 >> /home/vagrant/hosts
fi

Usage:

To add a new site type

./hostupdater.sh 192.168.33.12 test1.com

To remove the site type

./hostupdater.sh del test1.com

Advertisements

Setting up wordpress test environment

Step 1 : Getting your virtual environment up and running.

cd ~/Public
mkdir faisal
cd faisal
git clone https://github.com/scotch-io/scotch-box.git .
vagrant up
vagrant ssh

Step 2: Install SVN to get test wordpress codebase from wordpress.

sudo apt-get update
sudo apt-get install subversion

Step 3: Install phpUnit to run your unit tests

curl -O https://phar.phpunit.de/phpunit-4.8.phar
sudo chmod +x phpunit-4.8.phar
sudo mv phpunit-4.8.phar /usr/local/bin/phpunit
phpunit --version

Step 4: Install WP-CLI to setup the test environment

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
sudo chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp-cli
wp-cli --version

Step 5: Setting up the test plugin folder

curl -O https://wordpress.org/latest.tar.gz
tar xvzf latest.tar.gz .
cd wp-content/plugins/
mkdir demo
cd demo
touch demo.php
wp scaffold plugin-tests demo

Step 6: Setting up the test environment

cd ~/Public/faisal
bash bin/install-wp-tests.sh wordpress_test root root localhost latest

Step 6: Check if test is running or not.

cd ~/Public/faisal/wp-content/plugins/demo/
phpunit

You should now see something similar to this –

Converting virtual-box appliances into vagrant box

It is possible to convert virtual box appliances to vagrant box without much of a hassle. To do so, first you have to import the appliance into your virtual box.

Once imported you can see the VM ID from the virtual media manager.You have to type in the following command in terminal to see the list of virtual boxes and their VM IDs.


vboxmanage list vms

Once you have the VM ID type in the following commands in terminal to export it as a box using vagrant. For example if the VM ID is “acef4c0a-35be-4640-a214-be135417f04d” then the command will be as follows –


vagrant package --base acef4c0a-35be-4640-a214-be135417f04d --output newbox.box

Once the box file is generated you can import in vagrant using the following command –


vagrant box add newbox.box --name newbox

Now in your vagrantfile you can use this “newbox” as the box to be used.


Vagrant.configure("2") do |config|
  config.vm.box = "newbox"
  # ...
end

Reference: https://github.com/crohr/ebarnouflant/issues/7

Installing odoo 9 on vagrant

Step 1 : Place the vagrant file in a folder where you want to create the virtual instance.

Download Vagrant File

Step 2 : Start up vagrant instance.

vagrant up

Step 3 : SSH into the box.

vagrant ssh

Step 4 : Update the system.

sudo apt-get update

Step 5 : Add odoo key

wget -O - https://nightly.odoo.com/odoo.key | sudo apt-key add -

Step 6 : Add odoo distribution file location on source file


# add "deb http://nightly.odoo.com/9.0/nightly/deb/ ./" at the end of the source.list file and save
sudo nano /etc/apt/sources.list

Step 7 : Install odoo.

sudo apt-get install odoo

Step 8 : Login as postgres user.

sudo su - postgres

Step 9 : Create new postgres user. Give `odoo123` as password. (You can given any password as long as you update it on the openerp-server.conf accordingly.)

createuser odoo -U postgres -dRSP

Step 10 :  Create new database.

createdb odoodb

Step 11: Run the postgres console.

psql

Step 12 : Grant permission to odoo user for odoodb database.

GRANT ALL PREVELEGES ON DATABASE odoodb TO odoo;

Step 11 : Update openerp-server.conf located at /etc/odoo/openerp-server.conf.

db_host = localhost
db_port = 5432
db_user = odoo
db_password = odoo123

Step 12 : Restart odoo server.

sudo service odoo restart

Now you should open the browser and type in http://localhost:8069/ and should see the odoo default installation screen.

Simple bash script to create virtual hosts in apache

Wrote this bash script that automates the task of creating apache site config file and updating host file for the newly created host. Don’t forget to restart the apache server after the file is created and host file entry is made.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
#asks password so that it can go and perform action in root directories
su
echo "Please give the domain name :"
read domain

echo "Please give the IP address :"
read ipaddrs

if [ -d "/etc" ]; then
  if [ -d "/etc/apache2" ]; then
    if [ -d "/etc/apache2/sites-available" ]; then
    
        if [ -f "/etc/apache2/sites-available/"$domain".conf" ]; then
            
            echo "SITE ALREADY EXISTS"
            
        else
            echo "CREATE NEW SITE CONFIG FILE"
            
            cd "/etc/apache2/sites-available"
            touch $domain".conf"
            
            
            echo "CREATE ENTRY INSIDE THE SITE CONFIG FILE"
            
            echo "<VirtualHost *:80>" >> $domain".conf"
            echo '\t DocumentRoot "/home/'$domain'/public"' >> $domain".conf"
            echo "\t ServerName "$domain  >> $1".conf"
            echo "\t ServerAlias www."$domain >> $1".conf"
            
            echo '\t <Directory "/home/'$domain'/public">' >> $domain".conf"
                echo '\t\t allow from all' >> $domain".conf"
                echo '\t\t order allow,deny' >> $domain".conf"
                echo '\t\t AllowOverride All' >> $domain".conf"
            echo '\t </Directory>' >> $domain".conf"
            
            echo "</VirtualHost>"  >> $domain".conf"
            
            echo "CREATE HOST FILE ENTRY"
            cd "../../"
            echo $ipaddrs $domain >> hosts
            
            cd "../home"
			echo "CREATE HOME DIRECTORY"
			mkdir $domain
			cd $domain
			mkdir "public"
			cd "public"
			touch index.html
			echo "Welcome to "$domain >> index.html

			echo "HOST SETUP SUCCESSFULLY. PLEASE RESTART APACHE."
        fi
    else
        echo "ERROR: '/etc/apache2/sites-available' does not exists";
    fi
  fi
fi