Posts Tagged ‘Linux’

[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

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


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
tar -xzvf phpMyAdmin- -C /home/myuser/www
cd /home/myuser/www
mv phpMyAdmin- phpmyadmin
cd phpmyadmin
mkdir config
chmod o+rw config
cp config/
chmod o+w config/
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 /home/myuser/www/phpmyadmin>
        Options FollowSymLinks
        Order allow,deny
        Allow from all

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
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
import pip
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
     for i in installed_packages])
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檔,存放在任何你想要放的路徑下。

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

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


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


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


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


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


chmod +x
Part 2. 改寫crontab

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

crontab -e

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

01 19 * * * /home/myuser/db_backup/


service crond status