LoginSignup
20

More than 5 years have passed since last update.

まっさらなVPS(CentOS6)からApacheを使ってDjangoを動かすまでのメモ

Last updated at Posted at 2016-04-21

個人的に制作するサイトではVPSでDjangoを動かす事が多いです。これをAnsibleのplaybookにしようとしていますが、それをするにもひとまず作業をまとめておかなければならないので、(DBサーバーもwebサーバーもひっくるめて)1台でDjangoアプリを動かすときによく使う設定などをメモ程度に羅列しておきます。ある程度記憶を頼りに書いている部分があるので間違っているところがあるかも

  • Note: 記述しているコマンドは適時sudoをつけている場合があります
  • Note: Django側の設定はここには書かれていません
  • Note: 各設定ファイルは全行ではなく差分だけ書いている場合があります

初期設定

yumの更新

yum update

使いそうなツール

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel mysql-devel

日本語化
/etc/sysconfig/i18n

LANG="ja_JP.UTF-8"

ユーザー作成

useradd webapp
passwd webapp

sudo できるように

usermod -G wheel webapp
visudo
  • %wheel ALL=(ALL) ALL

鍵認証

ssh-copy-idを使う場合

ローカル作業

$ brew install ssh-copy-id
$ ssh-keygen -t rsa -v
$ ssh-copy-id webapp@xxx.xxx.xx

ssh-copy-idを使わない場合

サーバー側

mkdir ~/.ssh
chmod 700 ~/.ssh

ローカル

$ ssh-keygen -t rsa -v
$ chmod 600 ~/.ssh/id_rsa.pub
$ scp ~/id_rsa.pub webapp@xxx.xxx.xx:~/.ssh/authorized_keys

SSH設定

/etc/ssh/sshd_config

Port 22  # 22はデフォルト値なのでこれ以外にした方が安全
PasswordAuthentication no
PermitRootLogin no
service sshd restart

ファイアウォール

  • Note: CentOS 6の設定です。CentOS 7からは設定方法が変わっているようです

/etc/sysconfig/iptables

*filter
:INPUT    DROP    [0:0]
:FORWARD  DROP    [0:0]
:OUTPUT   ACCEPT  [0:0]
:SERVICES -       [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT
-A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -j SERVICES
-A INPUT -p udp --sport 53 -j ACCEPT
-A INPUT -p udp --sport 123 --dport 123 -j ACCEPT
-A SERVICES -p tcp --dport 22 -j ACCEPT
-A SERVICES -p tcp --dport 80 -j ACCEPT
-A SERVICES -p tcp --dport 443 -j ACCEPT
COMMIT
service iptables start
iptables -L

httpdインストール

yum install httpd
chkconfig httpd on
/etc/httpd/conf/httpd.conf
ServerTokens ProductOnly
ServerSignature Off
NameVirtualHost *:80
service httpd configtest
service httpd restart

pythonインストール

  • Note: ここではシステム上で作業するためのPythonを/usr/local/に、virtualenvでバージョン管理する際にベースとして指定するPythonを/opt/local/にインストールしています。

作業用 Python

  • Note: CFLAGS=-fPIC --enable-sharedはあとでmod_wsgiをmakeした時にエラーに成ったので追加しました
curl -O https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
tar xvzf Python-2.7.10.tgz
cd Python-2.7.10
./configure CFLAGS=-fPIC --enable-shared
make
make install

(再ログイン)

virtualenv用 Python

pythonbrewpyenvを使うとバージョン管理は楽になりそうですが、それらを導入するまでもないと思うので直接インストールします。

curl -O https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
tar xvzf Python-2.7.10.tgz
cd Python-2.7.10
./configure --prefix=/opt/local/python2.7.10
make
make install

作業用Pythonにpipをインストール

wget https://bootstrap.pypa.io/get-pip.py
/usr/local/bin/python get-pip.py

virtualenv と virtualenvwrapperをインストール

/usr/local/bin/pip install virtualenv
/usr/local/bin/pip install virtualenvwrapper
~/.bashrc
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
fi

Djangoアプリ用のvirtualenvを作成

mkvirtualenv project_name --python=/opt/local/python2.7.10/bin/python

mod_wsgiのインストールと設定

  • Note: /home/webapp/project_nameにDjangoのアプリが入っている想定です。wsgiスクリプトやstaticのpathは調整する必要があります。
  • Note: アプリのサンプル cd /home/webapp; git clone git@github.com:itkr/simpledjango.git project_name
  • Note: はじめyumでインストールしましたが、標準で入っているPython2.6.6が使われてエラーになってしまったためソースからインストールする方法を記載しました。(参考:readthedocs app won't start with apache and django

インストール

yum install httpd-devel
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.2.8.tar.gz -O mod_wsgi-4.2.8.tar.gz
tar xzf mod_wsgi-4.2.8.tar.gz
cd mod_wsgi-4.2.8
./configure --with-python=/usr/local/bin/python2.7  # さっきインストールした作業用Python
/etc/ld.so.conf
/usr/local/lib
ldconfig

設定

/etc/httpd/conf.d/wsgi.conf
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonPath /home/webapp/.virtualenvs/project_name/lib/python2.7/site-packages
/etc/httpd/conf.d/project_name.conf
<VirtualHost *:80>
    ServerName simpledjango
    ErrorLog /var/log/httpd/simpledjango.error_log
    CustomLog /var/log/httpd/simpledjango.access_log combined
    AddDefaultCharset UTF-8

    <Directory "/home/webapp/project_name">
        AllowOverride None
    </Directory>

    WSGIScriptAlias / /home/webapp/project_name/wsgi/production.py

    Alias /static/admin /home/webapp/.virtualenvs/project_name/lib/python2.7/site-packages/django/contrib/admin/static/admin
    Alias /static /home/webapp/project_name/statics/
</VirtualHost>
  • Note: アプリごとにvirtualenv環境を分けるにはこちらの方法を利用すると良いかもしれません → mod_wsgiでアプリごとにvirtualenv環境を設定する

  • Note: ソースを置くディレクトリによっては403エラーが出るかもしれません。その場合はchmod 705 /home/webapp/など、権限を変更しておきます

mysqlのインストール

yum install mysql-server
yum install mysql-devel
/etc/my.conf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0

character_set_server=utf8
default-storage-engin=InnoDB
innodb_file_per_table

[mysql]
default-character-set=utf8

[mysqldump]
default-character_set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
service mysqld start
mysql_secure_installation
chkconfig mysqld on

アプリを動かす準備

  • Note: pipでインストールするものはrequirements.txtなどに列挙しておきpip install -r requirements.txtのように実行した方が良いと思います
cd ~/project_name
workon project_name
pip install MySQL-python
pip install django
python manage.py migrate --run-syncdb --settings=settings.production

最後にブラウザからアクセスしてみる

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20