Posts Tagged ‘django’

[Tutorial] Deploy Apache + MySQL + Python on AWS EC2

Thursday, August 21st, 2014

First, choose any AMI(Amazon Machine Image) you like and lanuch it. In this tutorial, I will choose Amazon Linux AMI (64-bit) as example. If you choose other AMI, the circumstance might be quite different. I can’t guarantee the following tutorial steps are the same.

EC2 AMI: Amazon Linux AMI (64-bit)
last revision: 2014-08-21
here are the instructions:

Part 1. Add new user

after login server via SSH client, switch to root account first
sudo su
add a new user named "myuser"(change it as you want) in root group
useradd -s /bin/bash -m -d /home/myuser -g root myuser

set his password

passwd myuser
let us be able to login SSH via password without certification file
vim /etc/ssh/sshd_config

find the following line and change its value from no to yes

PasswordAuthentication yes

restart sshd to make change working

service sshd restart
give user the ability to use sudo command
visudo

find the following line and add the highlighted part.

root         ALL=(ALL)   ALL
myuser   ALL=(ALL)   ALL

if you don’t want to type password again while sudo, then you can change it as the following

myuser   ALL=(ALL)   NOPASSWD:ALL

okay! you can logout and login with the new user now.

Part 2. Install Stuff

To ensure that all of your software packages are up to date, process the following command first.
sudo su
yum update -y
install build tools
yum install make automake gcc gcc-c++ kernel-devel git-core -y
install LAMP stuff. This will install a lot of things, Apache, MySQL, blah, blah...
yum groupinstall -y "MySQL Database" "PHP Support"
yum install -y httpd-devel php-mysql

launch Apache and MySQL

service mysqld start
service httpd start
set MySQL root password and add a new user

set root password

mysqladmin -u root password {your password}

if you meet the error “ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: NO)”, then do the following steps to fix it.

service mysqld stop
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
mysql -u root
mysql > use mysql
mysql > UPDATE user SET Password=PASSWORD('{your password}') where USER='root';
service mysqld start

login root

mysql -u root -p

add a new user

GRANT ALL ON *.* TO myuser@localhost IDENTIFIED BY '{your password}';
install phpMyAdmin

you can just copy all of the following content and paste it on the console.

mkdir /home/tmp
mkdir /home/myuser/www
cd /home/tmp
wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/4.4.15.1/phpMyAdmin-4.4.15.1-all-languages.tar.gz
tar -xzvf phpMyAdmin-4.4.15.1-all-languages.tar.gz -C /home/myuser/www
cd /home/myuser/www
mv phpMyAdmin-4.4.15.1-all-languages phpmyadmin
cd phpmyadmin
mkdir config
chmod o+rw config
cp config.sample.inc.php config/config.inc.php
chmod o+w config/config.inc.php
chown -R myuser:root /home/myuser/

open session permission for phpmyadmiin

sudo chmod 777 /var/lib/php/session

install required libraries

yum install -y php libmcrypt libmcrypt-devel php-mcrypt php-mbstring
modify Apache configuration
vim /etc/httpd/conf/httpd.conf

find the following line and add the highlighted part.

User apache
Group apache
User myuser
Group root

add the following content on the end of configuration file

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /home/myuser/www
    ServerName localhost

    <Directory /home/myuser/www>
        Options FollowSymLinks
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /home/myuser/www/phpmyadmin>
        Options FollowSymLinks
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

restart Apache

service httpd restart

make server auto-start Apache & MySQL after EC2 reboot

sudo chkconfig mysqld on
sudo chkconfig httpd on

Part 3. Install Python Libraries

install mod_wsgi module(a bridge for Python(Django) & Apache, wsgi protocol, see here)
yum install -y mod_wsgi

You have to do the following routine to install mod_wsgi Again for specifing correctly apxs and python path.

cd /home/tmp
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.2.7.tar.gz
tar zvxf 4.2.7.tar.gz
cd mod_wsgi-4.2.7
./configure --with-apxs=/usr/sbin/apxs  --with-python=/usr/bin/python27
make; make install
install virtualenv (a tool to create isolated Python environments. see here)
pip install virtualenv
install Python MySQL connector(a bridge for Python and MySQL. see here)

Yes, you have to repeat the mysql-connector-python name. Don’t ask me why… ~”~

pip install --allow-external mysql-connector-python mysql-connector-python
install Django (a python framework, support wsgi, see here)
pip install django
pip install django==1.8.18 (assign version)
install BeautifulSoup (a HTML parser for python, see here)
pip install beautifulsoup4
install django-celery (a Distributed Task Queue solution, see here)
pip install django-celery
install python-gcm (a Python client for Google Cloud Messaging for Android, see here)
pip install python-gcm
install Pillow (a Python Imaging Library, see here)
yum install libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel
pip install Pillow
Django app for handling the server headers required for Cross-Origin Resource Sharing (CORS)
pip install django-cors-headers
view installed packages
python
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
print(installed_packages_list)
if you want to uninstall a package, type the following command
pip uninstall {package_name}

That’s all. Good Luck! ;-)

[Tutorial] install Apache + MySQL + Python on Windows

Friday, June 27th, 2014

OS version: Windows 7 64-bit Service Pack 1
last revision: 2014-06-27
here are the instructions:

# download & install XAMPP (you only can get 32-bit version for windows)
http://www.apachefriends.org/download.html
http://www.easyphp.org/download.php (another choice)

# install python 2.7.x (choose 32-bit, Because the Apache in XAMPP is 32-bit version)
(why not python 3.x? don’t ask me, google the question by yourself.)
http://www.python.org/download/releases/2.7.7/

# check python bit-version (run the following script in Python)
import struct; print( 8 * struct.calcsize("P"))
(should print out ‘32′)

# check python version (run the following script in Python)
import sys; print (sys.version)
(print out ‘2.7.7′ in this moment)

# download mod_wsgi (a bride for Apache and Python)
http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi
Choose a 32-bit version zipped file (Because the Apache in XAMPP is 32-bit version)
Then unzip it to get a mod_wsgi.so file.
or you can get it directly from here(I upload my mod_wsgi.so file to google drive.)

# copy mod_wsgi.so to the XAMPP installed path
i.e. if you install XAMPP in the path “D:\XAMPP”
then copy mod_wsgi.so to the path: “D:\XAMPP\apache\modules\”

# add load wsgi module script in Apache configuration (httpd.conf)
LoadModule wsgi_module modules/mod_wsgi.so

that’s all.
You can launch Apache & MySQL in XAMPP now.

good luck! ;-)

[IMPORT!!] the Apache bit-version must be matched with the Python bit-version, or you will get error while Apache trying to launch. They must be both 64-bit version or both 32-bit version.

================== extra ==================
# install Pydev plugin for Eclipse
1. select “Help” on top toolbar in Eclipse
2. select “install new software”
3. add a new site named Pydev , Location = http://pydev.org/updates
4. select “Windows” on top toolbar in Eclipse
5. Choose PyDev > Interpreters > Python Interpreter
6. add a new interpreter path: D:\Python27\python.exe

# install pip
1. visit “http://bootstrap.pypa.io/get-pip.py” and download it
2. then enter command line, type “python get-pip.py”
3. add the pip directory path(i.e. D:\Python27\Scripts\) in “PATH” variable (don’t ask me what is “PATH”…)
(if it shows up an error when you type “pip” in command line mode. you need to exit command line mode and reopen it to make pip script activated.)

# install virtualenv (virtualenv is a tool to create isolated Python environments. see here)
pip install virtualenv

# install MySQL Python connector(a bridge for Python and MySQL)
go to MySQL official download page
choose Microsoft Windows platform
download the MSI installer for python 2.7 and install it

# install Django (a python framework, support wsgi)
pip install django

# install BeautifulSoup (a HTML parser for python, see here)
pip install beautifulsoup4

# install django-celery (a Distributed Task Queue solution)
pip install django-celery

# install python-gcm (a google cloud messaging for Android push notification)
pip install python-gcm