install trac 1.1.2-dev on ubuntu 12.04

I tried to make a good tutorial to figure out, how to install trac 1.1.2-dev on ubuntu 12.04. This was mainly done as a walkthrough for me, and I hope that you ‘ll find nice too.

I asume that you have a fresh install of ubuntu 12.04, so you firstly have to do:

sudo apt-get update
sudo apt-get upgrade.

Out of our scope I am just saying that, I allways want to take the role of root, even on an ubuntu linux system so the first thing I do is:

sudo passwd root

after setting a password for a root user, you are able to do:

su

and then you are logged in a root session.

One more thing that helps me a lot is to set bash_autocompletion for root user. This is tricky and easy in Ubuntu. You have to edit the the .bashrc file in your home directory. For the root user is:

/root/.bashrc

so lets do it:

vi /root/.bashrc

and then just comment out the 3 last lines in this file and more accurately these 3 lines:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
 . /etc/bash_completion
fi

now we are ready to proceed to the installation guide.

install trac 1.1.2-dev on ubuntu 12.04

lets apt-get install all packages needed:

apt-get -y install python-setuptools python-genshi python-genshi-doc mysql-client-5.5 mysql-server-5.5 python-mysqldb python-babel python-docutils enscript subversion git apache2 libapache2-mod-python mercurial python-dev python-pygments

I hope that I haven’t forget a package 😛

Database preparation

At the time of installing mysql-server-5.5 you will be asked to set a root password for the database server. Choose one to enter. After the successful installation of mysql server and all the other package, lets prepare our database server for the trac installation.

use the mysql client you just installed:

mysql -u root -p

enter the root password you have chosen

In this section I have to say that for each trac project you will need a database, so lets the database a relative name. I named mine trac_myproject. So enter on mysql console the following commands:

CREATE DATABASE trac_myproject DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
GRANT ALL ON trac_myproject.* TO tracuser@localhost IDENTIFIED BY 'tracpass';
flush privileges;

tracuser and tracpass in the above example are just example names.

Now write down the db connection string, that you will need when you will create your first trac project.

mysql://tracuser:tracpass@localhost:3306/trac_myproject

As an advice I ‘ll say that you shouldn’t use the char @ as member of the password beacuse trac will fail to make the connection to the database.

Once we are ready with the database lets get our hands dirty with trac!

Trac latest dev and TracMercurial plugin installation

we have to get the sources for the trac and TracMercurial:

I got the clones of these repositories to my root directory:

cd /root

clone repositories

svn co http://svn.edgewall.org/repos/trac/trunk/
hg clone http://hg.edgewall.org/trac/mercurial-plugin#1.0

install trac

get into the directory trunk

cd trunk
python setup.py install

install TracMercurial

get into mercurial-plugin directory

cd ../mercurial-plugin
python setup.py bdist_egg

After this procedure a python egg file has been created for the plugin, which we will copy into our project’s folder later. After this procedure we can use trac commands to create environment to create projects and more.

Create our trac environment directory

We can now create our trac environment diractory. You can initialize anywhere you want. My opinion is to create a directory under /usr/share/ directory named trac. Under this directory you can deploy your trac projects. Keep in mind that this directory will be exposed to web access.

mkdir /usr/share/trac
trac-admin /usr/share/trac/myproject initenv

now you will  be asked for a name of the project and the db connection string for this project ( mysql://tracuser:tracpass@localhost:3306/trac_myproject )

chown -R www-data:www-data /usr/share/trac/myproject

with the trac-admin command we created and initialized a project environemnet. Now you can create and initialize as much projects as you want under /usr/share/trac directory

We changed the group and owner of the project directory to apache user (www-data) to the project because we will now setup apache with mod_python module to serve the trac projects.

Apache Configuration

Usually main apache configuration file is /etc/apache2/apache2.conf, and we will edit it a litle to use python module for apache.

vi /etc/apache2/apache2.conf

Add the following lines at the end of the file:

<Location "/trac">
SetHandler mod_python
PythonInterpreter main_interpreter
PythonHandler trac.web.modpython_frontend
PythonOption TracEnvParentDir /usr/share/trac
PythonOption TracUriRoot /trac
</Location>

You should now check if module mod_python is enabled. Check if a file python.load exists under /etc/apache2/mods-enabled/ directory. If it does not exist create it your self:

vi /etc/apache2/mods-enabled/python.load

and add this line to the file:

LoadModule python_module /usr/lib/apache2/modules/mod_python.so

 

Basic Authentication

If you want basic authentication via apache then add to the /etc/apache2/apache2.conf file the following lines:

vi /etc/apache2/apache2.conf

add the following

<LocationMatch "/trac/[^/]+/login">
 AuthType Basic
 AuthName "Trac"
 AuthUserFile /etc/trac/trac.htpasswd
 Require valid-user
</LocationMatch>

save and exit

mkdir /etc/trac
htpasswd -c /etc/trac/trac.htpasswd admin

enter a password for the admin user

Now restart apache:

service apache2 restart

You can now visit the http://your-ip/trac/ or if you want to visit it locally http://127.0.0.1/trac/

I installed trac to a remote server so I visit my projects by using it’s ip. Keep in mind that you should check for firewall settings to allow access to port 80.

Configure the project to use TracMercurial

now we should copy the python egg file we created when installing the TracMercurial plugin into project’s plugin directory.

cp /root/mercurial-plugin/dist/TracMercurial-1.0.0.3dev-py2.7.egg /usr/share/trac/myproject/plugins/.

the name of the egg may vary depending of the dev version.

now lets make a profile for the mercurial, because it will be needed at next steps.

vi /root/.hgrc

and add the following lines by your needs.

[ui]
username = Administrator <admin@trac>

Ofcourse this is a test environment, and we will create the repository inside the trac project directory. In an mercurial server environment you can include the actual repository directories.

mkdir -p /usr/share/trac/myproject/hg/
cd /usr/share/trac/myproject/hg/

now lets init a hg project

hg init (project-directory)
cd (project-directory)
------
(add some files)
------
hg add
hg commit -m 'Initial commit'

Our last configuration step is to configure project see the repository

vi /usr/share/trac/myproject/conf/trac.ini

check if these lines exist and if not, add them

[hg]
node_format = short
show_rev = yes
[components]
tracext.hg.* = enabled

then under [trac] tag edit the corresponding to match:

repository_dir = /usr/share/trac/myproject/hg/(project-directory)
repository_type = hg

now you should do one more time:

chown -R www-data:www-data /usr/share/trac/myproject
service apache2 restart

I hope that all work fine!!!

References:

Backup and restore MySQL – mysqldump a database – Grant Privileges

Back up a MySQL database

If you want to backup a MySQL database and you have access to shell, then you can easily do your job.

You can use mysqldump to create a simple backup of your database using the following syntax. 

mysqldump -u [username] -p [password] [databasename] > [backupfile.sql]

after executing this command the result will be the backupfile.sql which is the backup file for your database. I shouldn’t pass the password argument [password] in the command because it would be available to other users of the system. If you leave it blank mysql client will ask you to enter your password. If you want to connect to a remote host to make a backup then the only thing you should do is to add -h [hostname | ip address] parameters like the syntax below.

mysqldump -h [hostname] -u [username] -p [password] [databasename] > [backupfile.sql]

This will work supposing you have privileges as the user you want to connect from the remote site.

There are a lot of combinations to dump a MySQL database and you can explore all of them here.

Restore a MySQL database from a dump

To restore a database from a file produced by mysqldump there is an easy walkthrough. Its the reverse way you resulted the dump file. Get a look at the following syntax.

mysql -u [username] -p [password] [databasename ] < [backupfile.sql]

you can restore the dump to a different database  or to a different database server and this is a good option if you want to make a replica of your data to make some tests.

Grant Privileges on a user in MySQL

To grant the privileges to user you have to connect to mysql as user that have privileges to grant privileges to other user as root user do.

As you may already know, to connect to the MySQL server as root you have to execute this command:

mysql -u root -p

once again do not provide your password to the command line. Now to grant privileges to a user you must keep in mind that you should give privileges to user from a host to some database table(s). Keeping this in mind is easy to remember the syntax of the command.

GRANT ALL PRIVILEGES ON 'databasename'.'databasetable'  TO 'username'@'host';
and then do
FLUSH PRIVILEGES

If you want to grant privileges on all tables or all databases just use the asterisk (* ) as follows:

GRANT ALL PRIVILEGES ON 'databasename'.* TO 'username'@'host';

If you want this user to have access from every host / ip address then:

GRANT ALL PRIVILEGES ON 'databasename'.* TO 'username'@'%';

and after each statement that affects the privileges of a user on a database you have to FLUSH PRIVILEGES.

In case you want to give this user the privilege to grant privileges then you have to use on of the above, using extra: “with GRANT option” . For example use:

  GRANT ALL PRIVILEGES ON 'databasename'.* TO 'username'@'%' WITH GRANT OPTION;

this will result a super user on this database and this user can connect from every site and he is able to grant privileges to other users for this database. Do not forget to FLUSH PRIVILEGES.

Linux ps command tutorial

If you want to see a full description of the Linux ps command view this article

A common way to use the ps command is:

ps ux

which shows all the processes currently running under your username in an easy to read table format. The “u” specifies the format, and the “x” makes sure all processes under your user name are included.

The displayed table has the following colomns:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

which includes the user name, the process ID, percent of CPU used, percent of memory used, virtual and resident memory size, start time and accumulated time, and finally the command line used to start the process.

To include all processes of all users you would add the “-a” flag:

ps -aux

To see the processes running under another user name you would use the “U” flag. For example

ps U jjones

will list all processes run by user “jjones”.

Adding the “u” flag will list the processes in the user friendly format:

ps uU jjones

If you want to see a full description of the Linux ps command view this article