Archive for August, 2014

[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! ;-)

如何自動備份MySQL DB in Linux Server (AWS EC2)

Wednesday, August 20th, 2014
Part 1. 首先,建立一個名叫db_backup.sh的script檔,存放在任何你想要放的路徑下。
vi db_backup.sh

基本寫法 (雙引號要留著,輸出檔名最好寫完整的絕對路徑,例: /home/db_backup/xxxx.sql )

mysqldump -u root --p"{PASSWORD}" {DB_NAME} > {FILE_NAME}.sql

如要備份所有Table

mysqldump -u root --p"{PASSWORD}" --all-databases > {FILE_NAME}.sql

如要省略建立database及table及不需要輸出trigger,可以加入額外的參數

mysqldump -u root -p"{PASSWORD}" -c -n -t --skip-triggers {DB_NAME} > {FILE_NAME}.sql

如要使用GZip壓縮

mysqldump -u root -p"{PASSWORD}" {DB_NAME} | gzip > {FILE_NAME}.sql.gz

如要使用GZip壓縮並且幫輸出的檔名字尾加上日期

mysqldump -u root -p"{PASSWORD}" {DB_NAME} | gzip > {FILE_NAME}_$(date +%Y-%m-%d).sql.gz

script內容寫好儲存了之後,將其修改為可被執行

chmod +x db_backup.sh
Part 2. 改寫crontab

修改crontab (請注意你目前登入的帳號身份,因為每個account都會有自己的crontab,可別改錯了。):

crontab -e

在crontab裡加入下面這行 (這樣寫的意思代表每天的晚上7點01分(GMT時區),執行後面指定路徑的script。前五個數字(*)分別代表: [分] [時] [日] [月] [Weekday])

01 19 * * * /home/myuser/db_backup/db_backup.sh

檢查cron是否有在運作

service crond status

【食記】台南 阿婆魯麵

Friday, August 1st, 2014

這家店搬來台南之前就聽過,是一間還蠻有名的小店,但一直沒吃到,因為常常經過都沒開。直到最近的某一天下午接近晚上六點時,不小心騎車路過看到店還有開著,就跟我老婆兩個人一起去吃了。因為是第一次去,也不知道吃起來合不合胃口,所以我們只叫了一碗魯麵合吃看看。我老婆還蠻怕吃到那種湯頭有點甜的南部羹湯了。但這家的羹湯,我老婆吃了一口後,直接跟我說: 「要不要再叫一碗?」。因為味道真是不錯,羹湯不會太甜,勾芡也不會太稠,而且料蠻多的,滿滿一碗都是。於是我火速跟老闆娘說: 「再來一碗!」。沒想到老闆娘跟我說: 「阿! 不好意思,剩下的被外帶的客人訂走了耶,沒了喔」。聽到時,真是情天霹靂,怎麼才過一分鐘就賣完了~ @@。不過也還好啦,反正我已經是在地台南人了,不再是個死觀光客 XD,想再來吃還怕沒機會嗎?

羹麵裡的料真的蠻多的,有赤肉、木耳、白菜、紅白蘿蔔等等。老闆娘以為我們是外地人,還親切的問我們說: 「你們是台南人嗎?」。我說: 「我們去年中才搬來台南,不過還沒吃過台南的魯麵。」。老闆娘便跟我們簡單介紹了一下魯麵的由來。以前通常是結婚或者喜慶的時候,家裡才會準備一大鍋魯麵給大家一起吃,平常日子還吃不到哩。她說他們店已經開了幾十年了,從他阿嫲年輕時就在這裡開始賣了。那時候國華街這裡連馬路都沒有,只有黃黃的泥巴路。

下次有機會到台南國華街附近走走時,不妨來試看看吧~ 沒吃過的人吃了應該會覺得還蠻特別吧~ ^o^

地址: 台南市中西區國華街三段51之2號
電話: 06 220 4090
營業時間: 每天早上九點到晚上六點 (有時候會提前賣完。農曆初一、十五公休)