Don’t like Mercurial Server’s interface? Or need more control try Rhodecode
This is a complete guide to install and configure Rhodecode on Ubuntu 12.04 using MySQL as the database instead of sqllite:
Rhodecode is a awesome DVCS system which supports both Mercurial and Git and provides a better user interface and more functionality than hgweb.
This guide is for people like me who are primary a windows people but want to install a neat DVCS on their server with basic working knowledge of Linux command prompt.
Alright enough said lets get down with business
Step 1: Install Ubuntu 12.04 LTS ( 64 bit) on your server or VM
Make sure the network is working,try
ini@ubuntu:~$ ifconfig
To start with it should show something meaningful for you in front of eth0 see example below
eth0Link encap:EthernetHWaddr 08:00:27:31:80:6b
inet addr:10.10.1.202Bcast:10.255.255.255Mask:255.0.0.0
inet6 addr: fe80::a00:27ff:fe31:806b/64 Scope:Link
UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
RX packets:2816 errors:0 dropped:0 overruns:0 frame:0
TX packets:891 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:653933 (653.9 KB)TX bytes:329047 (329.0 KB)
loLink encap:Local Loopback
inet addr:127.0.0.1Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNINGMTU:16436Metric:1
RX packets:357 errors:0 dropped:0 overruns:0 frame:0
TX packets:357 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:25587 (25.5 KB)TX bytes:25587 (25.5 KB)
If not you need to setup your network, if you know how to do that on Ubuntu 12.04 then its great if not try this quick tutorial
I assume your network is working just fine and you are able to ping to the internet from your console.
Step 2: Grant yourself root access (though extremely dangerous due to the fact that there is no undo in linux by default, I prefer this as it is simply a PITA to type sudo everytime. I got rmed not a great experience anyways to use the system as root!! lol )
ini@ubuntu:~$ sudo su
[sudo] password for ini: enter your user password here
The prompt will change to something like this
root@ubuntu:~$
Step 3: is to Update/ upgrade your installation
Now we will make sure the Ubuntu installation is up to date
root@ubuntu:~$apt-get update
root@ubuntu:~$apt-get upgrade
Your Ubuntu installation will now search and download updates, wait for some time till both the commands complete.
Now let us install pip
root@ubuntu:~$ apt-get install python-pip
Now we are ready to install virtualenv, this creates a virtual environment for python so that if you run multiple instances with different versions, there will be no clash of versions between them. Using pip
root@ubuntu:~$ pip install virtualenv
Also install MySql (we want mysql in place for the database Rhodecode uses)
root@ubuntu:~$ apt-get install mysql-server
The following installs the python wrapper for mysql server, you need this to ensure you do not get MySQLdb not found
root@ubuntu:~$ apt-get install python-mysqldb
Some more dependencies ( maybe iam doing an overkill but does it matter ?)
root@ubuntu:~$ apt-get install libmysqlclient-dev
Step 4: Now we instgall RabbitMQ server
root@ubuntu:~$ apt-get install rabbitmq-server
Create a rabbit user
root@ubuntu:~$ rabbitmqctl add_user rhodeuser rhodepass
Add a vhost to RabbitMQ
root@ubuntu:~$ rabbitmqctl add_vhost rhodevhost
Step 5: let us installpython dev package
root@ubuntu:~$ apt-get install python-dev
Now we need to create the directory where everything belonging to Rhodecode will be installed, I name it rhode, you can make your choice
root@ubuntu:~$ cd /
root@ubuntu:/# mkdir /rhode
You are the root, but then again just to make sure I give temp ownership of the folder to my username (here ini is my username)
root@ubuntu:/# chown ini /rhode
now we need to make the directory structure inside , and install rhodecode, this is a bit different from windows install which I will post a tutorial as well, first setting up venv at /rhode/venv
root@ubuntu:/# virtualenv --no-site-packages /rhode/venv
you can also do
root@ubuntu:/# virtualenv /rhode/venv
now that you have created the venv lets activate it
browse to /rhode/venv/bin
root@ubuntu:/# cd /rhode/venv/bin
root@ubuntu:/rhode/venv/bin# source activate
Now you are in virtual env for rhodecode , the prompt should show something like this
(venv)root@ubuntu:/rhode/venv/bin#
Now go back to the root dir by doing
(venv)root@ubuntu:/rhode/venv/bin# Cd /
And install pastescript using pip
(venv)root@ubuntu:/# pip install pastescript
Then install rhodecode using
(venv)root@ubuntu:/# pip install rhodecode
This will take a lot of time for slow connections, be patient while it completes installation
The installation will show a lot of warnings, I ignored them all ( there were a lot of them and scrolling too fast to make anything out of them) another reason why I don’t really prefer linux for production use( though I like it’s speed and performance)
Step 6: Once this is done go to /rhode/data and execute the paster to make production.ini file
While in venv
(venv)root@ubuntu:/# cd /rhode/data/
And then
(venv)root@ubuntu:/rhode/data# paster make-config RhodeCode production.ini
Step 7: Make a mysql db before we go any further as we plan to use mysql for the database instead of the default sqllite, this is better for production use and a lot more reliable.
Let us create the mysql database and users, grant them permission
So type in
root@ubuntu:/# mysql –p root –u
mysql> create database rhodecode character set utf8;
mysql> create user 'rhodecode'@'localhost' identified by 'my_password';
mysql>grant all privileges on rhodecode.* to 'rhodecode'@'localhost';
mysql>flush privileges;
mysql>exit
Go back to venv if you are not in venv already or exited it due to some reason. Type in console:
root@ubuntu:/# /rhode/venv/bin/source activate
(venv)root@ubuntu:/#
Step 8: Now that you are in venv, let us find out the package called MySQLdb, this is actually the python wrapper for MySQL (though we installed it earlier through pip, it still showed me errors making paster run configure on production.ini
That’s why we take the harder way and build it again, this will add python into the system path, twice once for venv, once system wide, if it shows any warnings relating to double entry of path, ignore and go ahead with it as I did. Experienced linux guys would know better how to do it.
So now get the wrapper from its website, extract it and then install it.
(venv)root@ubuntu:/# wget http://ncu.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
(venv)root@ubuntu:/# tar –zxvf MySQL-python-1.2.3.tar.gz
(venv)root@ubuntu:/# cd MySQL-python-1.2.3
(venv)root@ubuntu:/MySQL-python-1.2.3# python setup.py build
The build should complete without errors
Now we install
(venv)root@ubuntu:/ MySQL-python-1.2.3# python setup.py install
Remember to be as the root all the time else it may fail.
Step 9: Now we are good to set up the production.ini file genertated by the paste script earlier.
So lets go to /rhode/data/
(venv)root@ubuntu:/ MySQL-python-1.2.3# Cd /rhode/data
now on to editing the production.ini
I use nano and completely hate vim or its clones or whatever ( sorry, its just because you have to spend rest of your live learning to use a text editor rather than getting things done)
I issue the following command while in the data directory as before
(venv)root@ubuntu:/rhode/data#nano production.ini
In there look for the following and make changes, this is crucial, so be careful, double check just in case, leave the host to 127.0.0.1 - ( thanks for the correction Marcin)
[server:main]
host = 127.0.0.1
you can also change the port= 5000 anything you please other than 80 and 443 , I dint do it.
Next, in the same file find CELERY CONFIG section in the same file. There change
use_celery = false
To
use_celery = true
Remove the line which looks like
Broker.host = localhost
Please don’t comment it out, in my experience sometimes commented sections are also read ( gitorious installation blues!) So I prefer to remove them, anyways its your choice, you have been warned.
Change broker.vhost = rabbitmqhost
to
broker.vhost = rhodevhost
Remember we added a vhost while setting up celery. Leave the broker.port as it is (unless you have done something earlier). Also change the subsequent directives to the following:
Broker.user = rhodeuser
Broker.password = rhodepass
Now go to the DB CONFIGS section in the same file and remove SQLLITE section and POSTGRESQL section.
Change sqlalchemy.db1.url = mysql://username:password@localhost/rhodecode
To
sqlalchemy.db1.url = mysql://rhodecode: my_password@localhost/rhodecode
Remember the database, user and password we created at the beginning of this tutorial, use them here.
This is pretty much what you want to do, you can play around other configs if you want but I haven’t tried them much, if anyone tries other configs please let me know your experiences as well.
Step 10: Now we are good to make rhodecode serve us fantastic hg and git
Generate the Rhodecode database using the following command (this is required every time you change the production.ini,and destroys the previous db, so use this with care, and avoid changing configs while in production, but anyways, your repos are always safe, don’t worry even if you want to change config again and again if you are the only user or only few people use it.
While in venv,
Grant the permissions to rhodeuser and rhodehost to make celeryd connect to rabbitmq
rabbitmqctl set_permissions -p rhodevhost rhodeuser ".*" ".*" ".*"
Run the paster setup
(venv)root@ubuntu:/ rhode/data# paster setup-rhodecode production.ini
This will now run (hopefully without any errors and ask you questions on admin username, password and location to store repositories which we made earlier which was: /rhode/repo/
Start celery using configuration from production .ini with the following command
(venv) paster celeryd production.ini &
Hit enter and then type the following command
(venv) paster serve production.ini
Fire up your browser if u have gui on Ubuntu and go to http://127.0.0.1:5000 or if on VM, go to http://your-vm-ip:5000
If everything is okay, you got Rhodecode up and running. Now that’s not enough, is it?
Step 11: On a production environment, you need a proper web server to proxy it, maybe have ssl as well.
If you want you can proxy this on apache2 or on nginx. Before we do anything further, you want to start rhodecode automatically when Ubuntu starts as a service or as a daemon (as they call it in Linux world to do that we need to create a user who can start it automatically.
Type in as root not in venv
adduser --no-create-home \
--disabled-login \
--system –group rhodecode
Give the new user rhodecode the ownership of /rhode directory
chown rhodecode:rhodecode-R /rhode
(this dint really work for me and I always started Rhodecode as root, if someone knows a not too difficult way to use this as user: rhodecode, please post them on the comments, I will be glad to use that)
Step 12: Now we need to create the init.d script to start rhodecode as daemon.
In the Rhodecode source, they guys have included neat daemon scripts for the major linux distros, since we are using Ubuntu, which is a debian distro, we select the file called rhodecode-daemon2. Here as a matter of fact don’t use any older scripts but the latest one from the sourcecode itself otherwise you will spend the rest of the day debugging the script ( it happened to me but then hey Iam a not a linux geek and all I wanted is to get this work)
You can download it from bitbucket repo of rhodecode by
wget https://bitbucket.org/marcinkuzminski/rhodecode/raw/51b203e44202/init.d/rhodecode-daemon2
Now the tricky part, edit this file
(venv)root@ubuntu:/# nano rhodecode-daemon2
lookup the following lines
APP_NAME="rhodecode"
APP_HOMEDIR="marcink/python_workspace"
APP_PATH="/home/$APP_HOMEDIR/$APP_NAME"
CONF_NAME="production.ini"
PID_PATH="$APP_PATH/$APP_NAME.pid"
LOG_PATH="$APP_PATH/$APP_NAME.log"
PYTHON_PATH="/home/$APP_HOMEDIR/v-env"
RUN_AS="marcink"
Change them as
APP_NAME="rhodecode"
APP_HOMEDIR="/rhode"
APP_PATH="/rhode/data"
CONF_NAME="production.ini"
PID_PATH="$APP_PATH/$APP_NAME.pid"
LOG_PATH="$APP_PATH/$APP_NAME.log"
PYTHON_PATH="/$APP_HOMEDIR/venv/bin"
RUN_AS="root" # this is not advisable but you can put in your username if #it works for you
Save the file and make it executable
(venv)root@ubuntu:/# chmod +x rhodecode-daemon2
Try running it
(venv)root@ubuntu:/# ./ rhodecode-daemon2 start
If it starts, check up rhodecode in your browser. I hope everything should be fine then stop it
(venv)root@ubuntu:/# ./ rhodecode-daemon2 stop
Now copy this script into the /etc/init.d/ directory and change its name to rhodecode ( I change the name since everything I use in this is either rhodecode and rhodepass)
(venv)root@ubuntu:/# cp ./rhodecode-daemon2 /etc/init.d/rhodecode
go into /etc/init.d
(venv)root@ubuntu:/# cd /etc/init.d/
Update the init scripts with rhodecode
(venv)root@ubuntu:/etc/init.d/# update-rc.d rhodecode defaults
just in case you want, you can try out the following command anywhere in the system
(venv)root@ubuntu:/# service rhodecode start
and
(venv)root@ubuntu:/# service rhodecode stop
Step 13: Great now everything till now is cool.
Some admins want LDAP support ( so that there is no more manual account creation and deletion of users and rights in Rhodecode)
We can install LDAP support by simply running the following command and then configuring it from the Rhodecode admin section inside the application ( that’s from the browser)
Coming back to nginx setup, lets get some prerequisites for nginx
root@ubuntu:/# apt-get install libpcre3-dev build-essential
If this fails (mine failed, try)
root@ubuntu:/# apt-get install libpcre3-dev build-essential --fix-missing
lets create the user for nginx service
root@ubuntu:/# adduser --no-create-home \
--disabled-login \
--disabled-password \
--system --group nginx
Now get the latest nginx from it’s website
root@ubuntu:/# wget http://nginx.org/download/nginx-1.3.1.tar.gz
Once downloaded extract it
root@ubuntu:/# tar –zxvf nginx-1.3.1.tar.gz
get inside the nginx directory
root@ubuntu:/# cd nginx-1.3.1
Configure the installer to setup with http and ssl modules (for https access)
root@ubuntu:/nginx-1.3.1# ./configure --prefix=/opt/nginx --with-http_ssl_module
Once successful, lets compile it by issuing make command
root@ubuntu:/nginx-1.3.1# make
Once done do an install
root@ubuntu:/nginx-1.3.1# make install
(Remember to be the root user all the way to avoid the sudo pain and other random errors!)
Once installed create SSL keys, please enter the following one by one in your console as root
root@ubuntu:/nginx-1.3.1# cd /opt/nginx/conf
root@ubuntu:/opt/nginx/conf# openssl genrsa -des3 -out server.key 1024
root@ubuntu:/opt/nginx/conf# openssl req -new -key server.key -out server.csr
root@ubuntu:/opt/nginx/conf# cp server.key server.key.orig
root@ubuntu:/opt/nginx/conf# openssl rsa -in server.key.orig -out server.key
root@ubuntu:/opt/nginx/conf# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
root@ubuntu:/opt/nginx/conf# rm server.csr
root@ubuntu:/opt/nginx/conf# chmod 600 server.crt server.key
Answer the questions the script pops up, this will make the security certificate for you to use once done remove the stock nginx.conf from :/opt/nginx/conf directory, well unless you know how to modify it to suit the purpose here, I removed and replaced it with the one below
root@ubuntu:/opt/nginx/conf# rm nginx.conf
Download this script from https://raw.github.com/gist/2866413/9103512672ac91c335edf642499f0a37decaba9e/nginx.conf
wget https://raw.github.com/gist/2866413/9103512672ac91c335edf642499f0a37decaba9e/nginx.conf
Once downloaded, fire up nano
root@ubuntu:/opt/nginx/conf# nano nginx.conf
And check if it is the correct file ( just incase!)
And download this config file
Wget https://raw.github.com/gist/2866413/5994c995631e49a22a9503864d2b1f855f9539ef/rhodecode.conf
Or else you can fire up nano and create this file with the following lines
root@ubuntu:/opt/nginx/conf# Nano rhodecode.conf
proxy_redirect off;
proxy_set_headerHost $host;
proxy_set_headerX-Url-Scheme $scheme;
proxy_set_headerX-Host $http_host;
proxy_set_headerX-Real-IP $remote_addr;
proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_headerProxy-host $proxy_host;
client_max_body_size 400m;
client_body_buffer_size 128k;
proxy_bufferingoff;
proxy_connect_timeout 7200;
proxy_send_timeout 7200;
proxy_read_timeout 7200;
proxy_buffers8 32k;
Now we are good to test nginx, and if it is passing rhodecode to ssl
First start the Rhodecode service (else you will see bad gateway error from nginx)
As root:
root@ubuntu:/# service rhodecode start
start nginx
root@ubuntu:/# /opt/nginx/sbin/nginx
Open up your browser and go to https://your-vm-ip/
You should see Rhodecode instance running, now stop the service with the following command
root@ubuntu:/# /opt/nginx/sbin/nginx -s stop
Or
root@ubuntu:/# /opt/nginx/sbin/nginx stop
Then stop rhodecode service
root@ubuntu:/# sudo service rhodecode stop
Now it is clear that nginx works as intended ( the http wont work as we dint configure it)
Step 14: Install nginx as a daemon, download nginx-init.d.sh from https://raw.github.com/gist/2866413/a62cc8ba4d86e06c138d9fc27b580ff2759e3bfe/nginx-init.d.sh
root@ubuntu:/# wget https://raw.github.com/gist/2866413/a62cc8ba4d86e06c138d9fc27b580ff2759e3bfe/nginx-init.d.sh
No need to edit it as it works just fine for but just in case check for the PREFIX=/opt/nginx
Now we can install the script so that it starts at boot.
root@ubuntu:/# sudo cp ./nginx-init.d.sh /etc/init.d/nginx
root@ubuntu:/# cd /etc/init.d
root@ubuntu:/etc/init.d# sudo update-rc.d nginx defaults
You may want to test the script again by running
root@ubuntu:/etc/init.d# service nginx start
Note this time since rhodecode is not running, you will get a bad gateway error when you navigate to https://your-vm-ip
stop the nginx service
root@ubuntu:/etc/init.d# service nginx stop
Go for a reboot and once at login prompt ( no need to login), fireup firefox and check on https://your-vm-ip if Rhodecode is working fine
Congratulations, you have a working DVCS repo with all production grade functionalities ( you however may need to setup a firewall to block http://your-ip:5000 from being directly accessible, at Excedalogic we use it on hyper-v and the host system using Windows Server 2008 R2has a firewall configured for that purpose ( it does the job very well)
UPDATE: Marcin Kuzminski suggested changes to the earlier setup in step 9 instead of changing the host = 127.0.0.1 to 0.0.0.0, let it remain at 127.0.0.1, this way Rhodecode will only be accessable to the machine running it and no need to setup a firewall.
Phew.. that wasn’t too hard however I could not have written this without the great guide at https://gist.github.com/2866413 , another one about the MYSQLdb errors at http://crucial-systems.com/installing-mysqldb-ubuntu-mysql-python
I will upload a VM as well (VBOX VHD) or put up a torrent if anyone wants it.
Thanks for your time, please contribute to the discussion here and share this tutorial.
Suggestions, feedbacks are always welcome to refine this guide and share your experiences with Rhodecode.
Ini karan, CEO Excedalogic IT Services India Private Limited
www.excedalogic.com