一个服务器的初建(CentOs+Nginx+MySQL+Django)

1、 安装vim

yum install vim

2、 安装tmux

yum install tmux

3、 修改ssh端口号(两个文件分别对应服务端和客户端)

vim /etc/ssh/sshd_config
service sshd restart

4、创建新用户

useradd newuser
passwd newuser

5、 增加新用户的sudo权限

vim /etc/sudoers
newuser ALL=(ALL) ALL

6、禁止root用户远程登录

vim /etc/ssh/sshd_config
PermitRootLogin no
service sshd restart

7、检查iptables防火墙,如果开启,将新的ssh端口号加入白名单

service iptables status

8、安装gcc

yum install gcc

9、安装python-dev

yum install python-devel

10、安装pip

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python get-pip.py

11、安装virtualenv

pip install virtualenv
mkdir myvirtual
cd myvirtual
virtualenv env1
source bin/activate

12、安装Django

pip install django

13、安装MySQL(MariaDB)

yum install mysql
yum install mariadb-devel
yum install mariadb-server
/usr/bin/mysql_install_db
chown -R mysql.mysql mysql
chown -R mysql.mysql test
chown -R mysql.mysql performance_schema/
/usr/bin/mysqld_safe --datadir="/var/lib/mysql" --socket="/var/lib/mysql/mysql.sock" --pid-file="/var/run/mariadb/mariadb.pid" --basedir=/usr --user=mysql

14、更改mysql的root用户密码

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('password');

15、安装Mysql-python

yum install MySQL-python

16、安装Nginx

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
service nginx start

17、配置Nginx

vim /etc/nginx/conf.d/default.conf

配置文件内容:

upstream myproject_server {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response (in case the Unicorn master nukes a
    # single worker for timing out).
    server unix:/srv/myproject/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name your_domain.com;
    client_max_body_size 4G;

    access_log /srv/myproject/logs/nginx-access.log;
    error_log /srv/myproject/logs/nginx-error.log;

    location /static/ {
        alias /srv/myproject/static/;
    }

    location /media/ {
        alias /srv/myproject/media/;
    }

    location / {
        # an HTTP header important enough to have its own Wikipedia entry:
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # enable this if and only if you use HTTPS, this helps Rack
        # set the proper protocol for doing redirects:
        # proxy_set_header X-Forwarded-Proto https;

        # pass the Host: header from the client right along so redirects
        # can be set properly within the Rack application
        proxy_set_header Host $http_host;

        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;

        # set "proxy_buffering off" *only* for Rainbows! when doing
        # Comet/long-poll stuff. It's also safe to set if you're
        # using only serving fast clients with Unicorn + nginx.
        # Otherwise you _want_ nginx to buffer responses to slow
        # clients, really.
        # proxy_buffering off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://myproject_server;
            break;
        }
    }

    # Error pages
    # error_page 500 502 503 504 /500.html;
    # location = /500.html {
    # root /home/demo/webapps/myproject/static/;
    # }
}

重启Nginx:

service nginx restart

18、安装gunicorn

pip install gunicorn 

gunicorn启动脚本

#!/bin/bash

NAME="myproject" # Name of the application
DJANGODIR=/srv/myproject/myproject # Django project directory
SOCKFILE=/srv/myproject/run/gunicorn.sock # we will communicte using this unix socket
USER=root                                                   # the user to run as
GROUP=root # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=myproject.settings # which settings file should Django use, change to
DJANGO_WSGI_MODULE=myproject.wsgi # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec ../bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --log-level=debug \
  --bind=unix:$SOCKFILE \
  --pythonpath=$PROJECTDIR

19、安装supervisor

yum install supervisor
vim /etc/supervisord.d/default.ini

配置文件内容:

[program:myproject]
command = /srv/myproject/bin/gunicorn_start ; Command to start app
user = root ; User to run as
stdout_logfile = /srv/myproject/logs/gunicorn_supervisor.log ;
redirect_stderr = true ; Save stderr in the same log
environment = LANG = en_US.UTF-8,LC_ALL = en_US.UTF-8;
autostart = true
autorestart = true

重启supervisor

service supervisord restart

20、安装git

yum install git