Ubuntu 14.04系统上安装和配置Nginx/MySQL/PHP/Python的一些笔记。
- 初次发布 - 2014-06-30 14:00
- 更新时间 - 2015-12-22 16:00
SSH配置
登录服务器,配置SSH
添加用户
1 2
| adduser mcxiaoke gpasswd -a mcxiaoke sudo
|
生成公钥私钥对
复制公钥到服务器
手动服务公钥到服务器
1 2 3 4 5 6 7 8 9 10
| cat ~/.ssh/id_rsa.pub
su - mcxiaoke mkdir .ssh chmod 700 .ssh vim .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
|
使用公钥登录
修改SSH配置
1 2 3 4 5 6
| vim /etc/ssh/sshd_config
Port 2222 PermitRootLogin no PermitEmptyPasswords no PasswordAuthentication no
|
重启SSH服务
软件包
1 2 3 4 5 6 7 8 9
|
apt-get install sudo
sudo apt-get update
sudo apt-get install curl wget vim git
|
安装LEMP软件包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #安装nginx sudo apt-get install nginx
# 测试nginx安装成功,假设IP为 8.8.8.8 # 浏览器访问 http://8.8.8.8 确认看到 Welcom to nginx!
# 安装MySQL,可选 sudo apt-get install mysql-server # MySQL初始化配置 sudo mysql_install_db # MySQL安全配置 sudo mysql_secure_installation
# 安装PHP,可选 sudo apt-get install php5-fpm # sudo apt-get install php5-mysql # 修改PHP配置 sudo vim /etc/php5/fpm/php.ini # 修改这一行 cgi.fix_pathinfo=0 # 重启PHP服务 sudo service php5-fpm restart
|
Nginx配置
配置Nginx支持PHP
1
| sudo vim /etc/nginx/sites-available/default
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| server { listen 80 default_server; listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html; index index.php index.html index.htm;
server_name ssl.mcxiaoke.com;
location / { try_files $uri $uri/ =404; }
error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
|
配置完成后,测试一下
1 2 3 4 5 6 7 8 9 10
| sudo service nginx restart
sudo vim /usr/share/nginx/html/info.php
sudo service php5-fpm restart sudo service nginx restart
|
浏览器访问 http://ssl.mcxiaoke.com/info.php ,如果出现了PHP信息表格,表明配置没有问题,安全起见,建议移除info.php文件。
nginx也支持自动列目录文件的功能
1 2 3 4 5
| location /public { autoindex on; autoindex_exact_size off; autoindex_localtime on; }
|
Flask配置
安装软件包
1 2
| sudo apt-get install build-essential python-dev python-pip sudo pip install uwsgi virtualenv flask
|
创建示例应用
1 2 3 4
| virtualenv myapp cd myapp source bin/activate pip install uwsgi flask
|
创建一个简单的Flask应用,文件名 myapp.py
1 2 3 4 5
| from flask import Flask app = Flask(__name__) @app.route('/') def helloworld(): return 'hello, world.'
|
Gunicorn
1 2 3 4
| pip install gunicorn
gunicorn myapp:app -b localhost:8000
|
或者创建一个 gunicorn_start
的bash
脚本文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #!/usr/bin/env bash
NAME="HelloFlask"
FLASKDIR=/home/mcxiaoke/myapp VENVDIR=/home/mcxiaoke/myapp SOCKFILE=//home/mcxiaoke/myapp/app.sock USER=mcxiaoke GROUP=mcxiaoke NUM_WORKERS=1
echo "Starting $NAME"
cd $VENVDIR source bin/activate
export PYTHONPATH=$FLASKDIR:$PYTHONPATH
RUNDIR=$(dirname $SOCKFILE) test -d $RUNDIR || mkdir -p $RUNDIR
exec gunicorn myapp:app -b 127.0.0.1:8000 \ --name $NAME \ --workers $NUM_WORKERS \ --user=$USER --group=$GROUP \ --log-level=debug \ --bind=unix:$SOCKFILE
|
执行权限
1 2 3
| chmod a+x gunicorn_start
./gunicorn_start
|
创建应用的nginx站点配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 80 default_server; server_name myapp.mcxiaoke.com; location / { proxy_pass http://127.0.0.1:8000; } }
|
测试一下
1 2 3 4
| sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo service nginx restart
|
打开浏览器,如果能看到页面就说明没问题了。
Supervisor
1 2
| sudo apt-get install supervisor
|
创建一个supervisor配置文件
1 2 3 4
| [program:myapp] command = gunicorn myapp:app -b localhost:8000 directory = /home/mcxiaoke/myapp user = mcxiaoke
|
读取并执行任务
1 2 3 4
| sudo pkill gunicorn sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start myapp
|
参考资料
How To Install LEMP stack on Ubuntu 14.04
How To Set Up Nginx Server Blocks on Ubuntu 14.04 LTS
How to Configure Nginx
Nginx Full Example Configuration
Kickstarting Flask on Ubuntu - Setup and Deployment
Comments