Sunday, April 30, 2017

Create a service for executable in Ubuntu 16.04 systemd



I am using a web server that I found in here as my executable, many thanks for the developer who developed this web server.

First of all we need to create a script which is capable of starting and stopping the executable and manage a PID file as we start and stop the executable, because systemd will need a PID file to find the main process id of the program.

Parameters


1. so my executable name is http_examples
2. my program is currently locate in /home/altair/Desktop/server/Simple-Web-Server/build
3. and I need to run this program as user altair
4. Start/Stop Script name server_startscript.sh

I will use above four parameters in this tutorial and you need to change them according to your environment, I will highlight them using red color so you can identify what places you need to edit



Creating the start/stop script

1
2
cd /home/altair/Desktop/server/Simple-Web-Server/build
vi server_startscript.sh

Paste the following, make sure to replace the BINARYNAME , press "I" before you paste the code because in VIM you need to press I to insert text to the file or use any other text editor like nano
  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#!/bin/sh

COMMANDLINE_PARAMETERS="${2}" #add any command line parameters you want to pass here
D1=$(readlink -f "$0")
BINARYPATH="$(dirname "${D1}")"
cd "${BINARYPATH}"
LIBRARYPATH="$(pwd)"
BINARYNAME="http_examples"


case "$1" in
 start)
  if [ -e server.pid ]; then
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "The server is already running, try restart or stop"
    exit 1
   else
    echo "server.pid found, but no server running. Possibly your previously started server crashed"
    echo "Please view the logfile for details."
    rm server.pid
   fi
  fi
  if [ "${UID}" = "0" ]; then
   echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
   c=1
   while [ "$c" -le 10 ]; do
    echo -n "!"
    sleep 1
    c=$(($c+1))
   done
   echo "!"
  fi
  echo "Starting the  server"
  if [ -e "$BINARYNAME" ]; then
   if [ ! -x "$BINARYNAME" ]; then
    echo "${BINARYNAME} is not executable, trying to set it"
    chmod u+x "${BINARYNAME}"
   fi
   if [ -x "$BINARYNAME" ]; then
    export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"     
    "./${BINARYNAME}" ${COMMANDLINE_PARAMETERS} > /dev/null &
     PID=`pidof ${BINARYNAME}`
    ps -p ${PID} > /dev/null 2>&1
    if [ "$?" -ne "0" ]; then
     echo "  server could not start"
    else
     echo $PID > server.pid
    fi
   else
    echo "${BINARNAME} is not exectuable, cannot start   server"
   fi
  else
   echo "Could not find binary, aborting"
   exit 5
  fi
 ;;
 stop)
  if [ -e server.pid ]; then
   echo -n "Stopping the   server"
   if ( kill -TERM $(cat server.pid) 2> /dev/null ); then
    c=1
    while [ "$c" -le 300 ]; do
     if ( kill -0 $(cat server.pid) 2> /dev/null ); then
      echo -n "."
      sleep 1
     else
      break
     fi
     c=$(($c+1)) 
    done
   fi
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "Server is not shutting down cleanly - killing"
    kill -KILL $(cat server.pid)
   else
    echo "done"
   fi
   rm server.pid
  else
   echo "No server running (server.pid is missing)"
   exit 7
  fi
 ;;
 restart)
  $0 stop && $0 start ${COMMANDLINE_PARAMETERS} || exit 1
 ;;
 status)
  if [ -e server.pid ]; then
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "Server is running"
   else
    echo "Server seems to have died"
   fi
  else
   echo "No server running (server.pid is missing)"
  fi
 ;;
 *)
  echo "Usage: ${0} {start|stop|restart|status}"
  exit 2
esac
exit 0

Save the file. Try to run and stop the executable by using the script.
1
2
chmod +x server_startscript.sh
./server_startscript.sh start

you will see a file called server.pid is created inside the directory. lets stop the executable.
1
./server_startscript.sh stop

Now the server.pid file should be removed from the files.

Creating the systemd service


1
sudo vi /lib/systemd/system/http_examples.service

add the following content and make sure to replace the highlighted areas
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[Unit]
Description=http_examples
After=network.target
 
[Service]
WorkingDirectory=/home/altair/Desktop/server/Simple-Web-Server/build/
User=altair
Group=altair
Type=forking
ExecStart=/home/altair/Desktop/server/Simple-Web-Server/build/server_startscript.sh start
ExecStop=/home/altair/Desktop/server/Simple-Web-Server/build/server_startscript.sh stop
PIDFile=/home/altair/Desktop/server/Simple-Web-Server/build/server.pid
RestartSec=15
Restart=always
 
[Install]
WantedBy=multi-user.target

Once done, save and close the editor then enable the server and startup script with this command
1
2
3
sudo systemctl --system daemon-reload
sudo systemctl enable http_examples.service
sudo service http_examples start

That's it comment if you got any problems

Thursday, April 20, 2017

Install Redmine Project Management tool in Ubuntu 16.04

Install Redmine Project Management tool on Ubuntu 16.04



First of all we need to update our system ( run these as root )

1
2
apt update
apt upgrade


Install the passenger and the Nginx web server

1
2
3
4
5
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
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
sudo apt-get install -y nginx-extras passenger

Setup MySql Server


1
2
3
4
5
6
sudo apt-get install mysql-server
mysql -p  #Enter the root password that you have entered at the setup of the mysql server
create database redmine; #Type in the mysql console
grant all privileges on redmine.* to 'red'@'%' identified by "123"; #Type in the mysql console
flush privileges;  #Type in the mysql console
exit;  #Type in the mysql console


Install prerequisites

1
apt install libmysqlclient-dev imagemagick libmagickwand-dev libcurl4-openssl-dev git-core subversion libssl-dev libreadline-dev


Creating a non sudo user to run the ruby project

1
2
useradd -m redmine -g www-data
passwd redmine


login to the redmine user account

1
su --login redmine


Install Ruby version 2.3.0

1
2
3
4
5
6
7
8
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile
echo 'eval "$(rbenv init -)"' >> ~/.profile
source ~/.profile
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 2.3.0
rbenv rehash
rbenv global 2.3.0

Get the redmine current stable version ( 2017-04-20 ) from the SVN

1
2
3
4
svn co https://svn.redmine.org/redmine/branches/3.3-stable redmine-3.3
cd redmine-3.3
cp config/configuration.yml.example config/configuration.yml
cp config/database.yml.example config/database.yml


Edit the database configuration file (Change production settings  username,databasename,password,host)


1
vi config/database.yml



Configure the redmine


1
2
3
4
5
gem install bundler
bundle install --without development test
bundle exec rake generate_secret_token
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake redmine:load_default_data

Configure the nginx


1
2
exit
vi /etc/nginx/sites-available/default


change your vhost configuration like this ( change or add the highlighted fields remove any other unnecessary fields )



1
vi /etc/nginx/nginx.conf

and uncomment
include /etc/nginx/passenger.conf;



1
service nginx reload

Now browse the web server using web browser,
default username and password is admin admin


Angular cli 4 JQuery \ Other Plugins(Underscore js) setup

For this blog I will show you how to setup JQuery and Underscore js with using NPM and without using NPM Lets create a new Angular cli pro...