Friday, October 21, 2011

Deploying Django

I can either do it the right way or the cheating way in screen, that might crash Django when certain errors are encountered. The screnning way might need this to keep the process running when I logout


TODO, suggested by mark:
with apache in front, mod_wsgi handles the processes for you
.. and standard server config takes care of the apache stuff

----

SSL .. Django admin through port forwarding?

Setting up MSW on Rackspace

./manage.py shell

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

In root install: pip install mysql-python.

If you I got another error ...
In file included from _mysql.c:29:0:
pymemcompat.h:10:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
... then you need to install sudo apt-get install python-dev

(I searched for solutions for a long time, trying to find my "/usr/local/mysql" on ubuntu, but I don't have it (even though I have mysql).)


Now ./manage.py shell has
ImportError: No module named OpenSSL
From root, pip install pyopenssl


Now ./manage.py shell has
ImportError: No module named bycrypt
From root, pip install py-bcrypt

AND THEN I GOT MANAGE.PY SHELL!!!
import django! works on it too!!!!!!!!
$ ./manage.py shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django

Whoa I can even run it on my rackspace IP! :D Even though it crashes, at least it can get the request!

$ ./manage.py runserver 50.57.147.82:8000
Validating models...

0 errors found
Django version 1.3.1, using settings 'MozSecWorld.settings_local'
Development server is running at http://50.57.147.82:8000/
Quit the server with CONTROL-C.
[21/Oct/2011 03:23:36] "GET / HTTP/1.1" 301 0

Traceback (most recent call last):
...
TemplateSyntaxError: Caught ImportError while rendering: No module named jinja2
[21/Oct/2011 03:23:37] "GET /msw/ HTTP/1.1" 500 2909
So I did, from root, pip install jinja2

AND MOZSECWORLD IS UP at http://50.57.147.82:8000/!!!!!!!! :D:D:D

Setting up more Django on a clean Ubuntu server

Get virtualenvs and virtualenvwrapper following this and this and this (with an informative video)

On root account:
1. sudo apt-get install python-setuptools

2. sudo easy_install virtualenv

3.
virtualenv ve_msw
New python executable in ve_msw/bin/python
Installing setuptools............done.
Installing pip...............done.

4. easy_install pip (I could also have done "pip install virtualenv" or "easy_install virtualenvwrapper"

5. pip install virtualenvwrapper

On user account:
6. mkdir ~/.virtualenvs

7. Add to end of ~/.bashrc:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

8.
$ mkvirtualenv ve_msw
New python executable in ve_msw/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/haoqili/.virtualenvs/ve_msw/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/haoqili/.virtualenvs/ve_msw/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/haoqili/.virtualenvs/ve_msw/bin/preactivate
virtualenvwrapper.user_scripts creating /home/haoqili/.virtualenvs/ve_msw/bin/postactivate
virtualenvwrapper.user_scripts creating /home/haoqili/.virtualenvs/ve_msw/bin/get_env_details

9. Added to ~/.virtualenvs/postmkvirtualenv
proj_name=$(echo $VIRTUAL_ENV|awk -F'/' '{print $NF}')
mkdir $HOME/Projects/$proj_name
add2virtualenv $HOME/Projects/$proj_name
cd $HOME/Projects/$proj_name
easy_install readline ipython
pip install -e git://github.com/jbalogh/check.git#egg=check Jinja2==2.3.1 \
    MySQL-python==1.2.3c1 lxml==2.2.6 PIL

10. Now I can call workon ve_msw

MSW already have playdoh set up at the very beginning
11. To set up where virtualenvwrapper's "workon" takes us to add this to
~/.virtualenvs/ve_msw/bin/postactivate
cd ~/path/to/mswdir

Thursday, October 20, 2011

Set up new mysql user

CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password_for_new_username';
GRANT ALL ON *.* TO 'new_username'@'localhost';

source

rackspace todo

set up django: https://docs.djangoproject.com/en/1.3/intro/install/

show alex ssh. :D

Linux add user

adduser username
not useradd username

Story: 

######## Stage 1. Stuck & Confused ############

I'm was trying to set up my Rackspace server and I ran into some questions during my created user account.

From root, I added a user with "useradd alice". When I discovered that ~/ failed, I had to sudo mkdir /home/alice.

Q0: It's so strange that the user home dir has to be manually created

Then I logged in as alice, but found that the prompt is not customized and lacks color. Since useradd copied /etc/skel/.bashrc from root to alice, I thought that I can just "cp /etc/skel/.bashrc ~/.bashrc", but when I did that (with sudo), "echo $PS1" still shows the old original prompt settings.

Q1: How come the ~/.bashrc, identical in root and user alice, does not work in alice?

I even uncommented out "force_color_prompt=yes", but its still not working.

Q2: How come user alice needs sudo to edit ~/.bashrc in the first place when its permissions are (from ls -l):
        alice@simba:~$ ls -l ~/.bashrc
        -rw-r--r-- 1 root root 141 2011-10-20 05:19 /home/alice/.bashrc

Answer: Because the user and group of /home/alice/.bashrc are both set to "root" (The user is the one that matters here)

Then I tried to "sudo chown alice:alice ~/.bashrc", but was denied permission because I was on alice.


######### Stage 2. Found good resource ############

Then I read this informative article and learned:

useradd -D shows the defaults of useradd. You can change the shell to bash by specifying useradd -D -s /bin/bash . Then you can use useradd alice followed by passwd alice to set its password.  BUT!! This still doesn't automatically make /home/alice for you


######## Stage 3. Problems solved! ################

adduser alice worked beautifully
0: home dir created automatically :D
1: bashrc and xterm-color both work :D
2: home dir's files are automatically made with alice as the user
# adduser alice
Adding user `alice' ...
Adding new group `alice' (1001) ...
Adding new user `alice' (1001) with group `alice' ...
Creating home directory `/home/alice' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for alice
Enter the new value, or press ENTER for the default
        Full Name []: Alice P Hacker
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] y
... Why does it care about the room number? haha

Wednesday, October 19, 2011

Give user sudo

You have to edit the /etc/sudoers file, which starts off with a scary text: This file MUST be edited with the 'visudo' command as root. OMG!

So I do visudo but it takes me to the nano editor.
I want vim instead! So I did export EDITOR="vim", which if you want to be your default editor, add this line to ~/.bashrc.

Copy and modify root   ALL=(ALL:ALL) ALL

Thursday, October 13, 2011

Setting up rackspace server

First I would like to say: thank you Rackspace, you have given me back the fire spark in life that has been swallowed by the fire hose.  Your superb user experience with instant server setup and live technical chat support has been a very pleasant surprise.  I can get all that for just $0.015/hour ($10.8/month)?

Anyways ... the things I did/learned from setting up my first Rackspace server:

1. Setting up apache for Python. I do want to run some Django apps, so I followed steps 1 and 2 of a previous setup.

2. DNS ... When I set up a domain name inside rackspace, it's going to be an internal lookup. I'll give my registered domain on name.com the general rackspace name. Then rackspace is going to do an internal lookup to find my specific machine that hosts the domain name.

3. History is found in ~/.bash_history


I created a user from root. How come the user only has sh shell but not the better bash shell like root?
Edit: I think there is more to it: useradd -D --shell <shell-to-set>

4. Check the shell with ps or echo $SHELL

5. Check the shell a particular user is running in /etc/passwd, or view it like grep username /etc/passwd

6. So I changed my user's entry in /etc/passwd from /bin/sh to /bin/bash

7. Move /bin/bash to the top of /etc/shells


Now my user has bash, but it doesn't have .bashrc. There is more to it.

8. Copy root's /etc/skel directory. "The /etc/skel directory contains files and directories that are automatically copied over to a new user's home directory when such user is created by the useradd program." ~ source 

It turns out that I do have them copied, but I have to manually copy them again into ~/, which I'll have to create first, but I need sudo powers for my user, see next post.