个人云服务器简单配置

本地软件

安装oh-my-zsh,更好用的终端

sudo apt-get update
sudo apt-get install zsh -y
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
vi ~/.zshrc
# 在文件中添加下面两行
export LANG=zh_CN.UTF-8
export LC_ALL='zh_CN.utf8'

安装pip3,更新源

sudo apt-get install python3-pip python3-dev -y
mkdir ~/.pip
vi ~/.pip/pip.conf

# 添加如下内容
[global]
index-url = https://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

创建虚拟环境,并安装库

mkdir ~/pyenv
cd ~/pyenv
sudo pip3 install --upgrade pip
sudo pip3 install virtualenv
virtualenv -p python3 py1flask
cd py1flask
source bin/activate
mkdir proj
cd proj
pip3 install flask uwsgi

开启防火墙

# 查看可应用列表
sudo ufw app list
# 允许ssh登录
sudo ufw allow OpenSSH
# 开启防火墙
sudo ufw enable
# 查看防火墙状态
sudo ufw status

UFW Essentials: Common Firewall Rules and Commands

创建测试Flask应用

mkdir testproj1
cd testproj1
vim myproj.py
# 添加如下内容
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')
# 开启防火墙5000端口
sudo ufw allow 5000
# 启动应用后在 ip:5000 端口查看
python myproj.py

创建WSGI入口点

# 该文件告知uWSGI服务器如何与我们的应用程序交互
vim wsgi.py
# 添加如下内容
from myproj import app

if __name__ == "__main__":
    app.run()
# 测试
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app

配置uWSGI

vim myproj.ini
# 添加如下内容
[uwsgi]
module = wsgi:app

master = true
processes = 5

# 用Unix套接字做中介将请求从Nginx传给uWSGI
socket = myproj.sock
# 设置套接字用户权限
chmod-socket = 660
# 进程停止清空套接字
vacuum = true

# 保证进程信号的一致性
die-on-term = true

创建系统服务管理

sudo vim /etc/systemd/system/myproj.service
# 添加如下内容,如果修改WorkingDirectory还得修改Nginx的uwsgi_pass的sock
[Unit]
Description=uWSGI instance to serve myproj
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/pyenv/py1flask/proj/testproj1
Environment="PATH=/home/ubuntu/pyenv/py1flask/bin"
ExecStart=/home/ubuntu/pyenv/py1flask/bin/uwsgi --ini myproj.ini

[Install]
WantedBy=multi-user.target
# 开启服务,开机自动运行
sudo systemctl start myproj
sudo systemctl enable myproj
# 修改后重启
sudo systemctl restart myproj

安装Nginx

sudo apt-get install nginx -y
# 修改配置文件
sudo vim /etc/nginx/sites-available/myproj
# 添加如下内容,!!! 注意修改server_domain_or_IP !!!
server {
    listen 80;
    server_name server_domain_or_IP;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/ubuntu/pyenv/py1flask/proj/testproj1/myproj.sock;
    }
}
# 使能Nginx配置文件
sudo ln -s /etc/nginx/sites-available/myproj /etc/nginx/sites-enabled
# 移除默认配置文件
sudo mv /etc/nginx/sites-enabled/default ~/naginx-default
# 检查配置语法是否正确
sudo nginx -t
# 重启Nginx
sudo systemctl restart nginx
# 防火墙禁止5000端口访问
sudo ufw delete allow 5000
# 允许Nginx通信
sudo ufw allow 'Nginx Full'
# 即可访问 http://server_domain_or_IP

How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 16.04

远程 jupyter notebook

# 进入python虚拟环境
source ~/pyenv/py1flask/bin/activate
pip3 install jupyter
jupyter notebook --generate-config
cd ~/.jupyter
# 设置登录密码,并复制获得的sha1码
python3 -c "from notebook.auth import passwd ; print(passwd())"
# 设置信息,影响不大
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mykey.key -out mycert.pem
vim jupyter_notebook_config.py
# 添加如下内容,注意修改password为生成的sha1码
# Set options for certfile, ip, password, and toggle off
# browser auto-opening
c.NotebookApp.certfile = u'/home/ubuntu/.jupyter/mycert.pem'
c.NotebookApp.keyfile = u'/home/ubuntu/.jupyter/mykey.key'
# Set ip to '*' to bind on all interfaces (ips) for the public server
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'sha1:f356c1d30583:5e264e01134aea09d8bc78e2d30a9d30a7d843bb'
c.NotebookApp.open_browser = False

# It is a good idea to set a known, fixed port for server access
c.NotebookApp.port = 9999
# 防火墙允许访问端口
sudo ufw allow 9999
# 运行,然后访问 https://domain_or_ip:9999/
jupyter notebook --config=/home/ubuntu/.jupyter/jupyter_notebook_config.py

安装Redis

sudo apt-get install redis-server -y
redis-server # 6379
# 查看redis是否启动服务
ps -aef | grep redis

###
/etc/init.d/redis-server stop
/etc/init.d/redis-server start

安装MySQL

sudo apt-get install mysql-server
mysql -u root -p
# 新建数据库
CREATE DATABASE wx;
# SQLALCHEMY_DATABASE_URI = "mysql://user:passwd@localhost/wx?charset=utf8mb4"

屏蔽IP

# 15.15.15.51为要屏蔽的IP
sudo ufw deny from 15.15.15.51
# 或者
sudo iptables -A INPUT -s 15.15.15.51 -p TCP -j DROP

加密库crypto安装

sudo apt-get install build-essential libssl-dev libffi-dev python-dev -y
source ~/pyenv/py1flask/bin/activate
pip install cryptography
pip install crypto

celery与gunicorn启动

source ~/pyenv/py1flask/bin/activate
pip install celery gunicorn aiohttp
nohup celery -A wxapp.celery worker -B -l info -E &
nohup gunicorn -w 4 -b 127.0.0.1:8000 -k gaiohttp run:app &
# 查看PIDs
ps -ef | grep gunicorn | tr -s " "| cut -d " " -f 2
# gunicorn样例Nginx配置
# sudo vim /etc/nginx/nginx.conf
# add in the http{ }:
  server {
    listen 80;
    server_name 5izxy.cn xx.xx.xx.xx;
    access_log  /var/log/nginx/example.log;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

安装测试软件wrk

sudo apt-get install build-essential libssl-dev git -y
git clone https://github.com/wg/wrk.git
cd wrk
make
# move the executable to somewhere in your PATH, ex:
sudo cp wrk /usr/local/bin

wget https://raw.githubusercontent.com/squeaky-pl/japronto/master/examples/1_hello/hello.py
python hello.py
wget https://raw.githubusercontent.com/squeaky-pl/japronto/master/misc/pipeline.lua
wrk -t 1 -c 100 -d 2 -s pipeline.lua http://0.0.0.0:8080

安装Elasticsearch

# 安装Java依赖
sudo apt-get install default-jre
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo update-alternatives --config java
sudo vi /etc/environment
# 在文件末尾添加
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
# 保存并退出后
source /etc/environment
echo $JAVA_HOME
# 下载并安装elasticsearch,如果服务器下载慢,则在本地下载后再上传
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.0.deb
# 老版,暂时推荐老版: https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/2.3.1/elasticsearch-2.3.1.deb
sudo dpkg -i elasticsearch-5.3.0.deb
# 默认安装位置:/usr/share/elasticsearch/
# 默认配置文件位置:/etc/elasticsearch
# 默认启动脚本:/etc/init.d/elasticsearch
sudo systemctl enable elasticsearch.service

# 修改配置
sudo vi /etc/elasticsearch/elasticsearch.yml
# 添加如下内容
cluster.name: mycluster1
node.name: "My First Node"

index.number_of_shards: 1
index.number_of_replicas: 0
# 按自己服务器内存大小缩小使用内存,老版不用管,直接启动
sudo vi /etc/elasticsearch/jvm.options
# 替换
-Xms2g
-Xmx2g
# 为
-Xms256m
-Xmx256m
# if other error try:  sudo sysctl -w vm.max_map_count=262144
# 启动
sudo systemctl start elasticsearch
sudo service elasticsearch status
# 测试
curl -X GET 'http://localhost:9200'
curl -XGET 'http://localhost:9200/_nodes?pretty'
# 简单使用,增加条目
curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
# tutorial —— index of the data in Elasticsearch
# helloworld —— type
# 1 —— id of our entry under the above index and type
# 获取
curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
# 更改
curl -X PUT 'localhost:9200/tutorial/helloworld/1?pretty' -d '
{
  "message": "Hello People!"
}'
# 查看修改后效果
curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty'
# python简单使用
from elasticsearch import Elasticsearch
esclient = Elasticsearch(['localhost:9200'])
response = esclient.search(
index='social-*',
body={
    "query": {
        "match": {
            "message": "myProduct"
        }
    },
    "aggs": {
        "top_10_states": {
            "terms": {
                "field": "state",
                "size": 10
            }
        }
    }
}
)

卸载Elasticsearch

sudo  systemctl stop elasticsearch.service
ps -aef| gerp elastic
sudo apt-get --purge autoremove elasticsearch
sudo rm -rf /var/lib/elasticsearch/
sudo rm -rf /etc/elasticsearch
sudo find / -name "elasticsearch"

可视化Elasticsearch

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.3.0-amd64.deb
# 老版: https://download.elastic.co/kibana/kibana/kibana_4.5.3_amd64.deb
sudo dpkg -i kibana-5.3.0-amd64.deb
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable kibana.service
sudo systemctl start kibana.service
# 默认位置
# $KIBANA_HOME = /usr/share/kibana
# bin = /usr/share/kibana/bin
# config = /etc/kibana/kibana.yml
# data = /var/lib/kibana
# plugins = /usr/share/kibana/plugins
sudo vi /etc/kibana/kibana.yml
# 老版: sudo vi /opt/kibana/config/kibana.yml
# 添加如下内容
server.port: 5601
server.host: 0.0.0.0
sudo ufw allow from Your_IP to any port 5601
# 载入样例数据
wget https://download.elastic.co/demos/kibana/gettingstarted/shakespeare.json
curl -XPUT http://localhost:9200/shakespeare -d '
{
 "mappings" : {
  "_default_" : {
   "properties" : {
    "speaker" : {"type": "string", "index" : "not_analyzed" },
    "play_name" : {"type": "string", "index" : "not_analyzed" },
    "line_id" : { "type" : "integer" },
    "speech_number" : { "type" : "integer" }
   }
  }
 }
}
';
curl -XPOST 'localhost:9200/shakespeare/_bulk?pretty' --data-binary @shakespeare.json

# 查看索引
curl 'localhost:9200/_cat/indices?v'

PostgreSQL

sudo apt-get install postgresql libpq-dev postgresql-client postgresql-client-common
sudo -i -u postgres
createuser username -P --interactive
createdb testpython
psql

# pip install psycopg2

wordpress

# 需要将php5改为php或者php7
# 还需要以下安装
sudo apt-get install libapache2-mod-php
## 在配置文件中添加servername后要在wordpress网页控制台确认站点地址

绑定域名后,网站还是显示ip地址-CSDN论坛

  • 概览
sudo apt-get install apache2
sudo vi /etc/apache2/sites-available/000-default.conf
# just before the </VirtualHost> entry add # 改掉域名
ServerName x.5izxy.cn
<Directory /var/www/html/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

sudo apt-get install mysql-server mysql-client
mysql -u root -p
CREATE DATABASE wordpressdb;
CREATE USER wpuser@localhost IDENTIFIED BY 'xxxxxxxx';
GRANT ALL ON wordpressdb.* TO wpuser@localhost;
FLUSH PRIVILEGES;
exit

sudo apt-get install php php-gd php-ldap php-xmlrpc php-mcrypt php-common php-snmp php-curl php-mysql libapache2-mod-php

sudo /etc/init.d/apache2 restart
sudo /etc/init.d/mysql restart

sudo wget http://wordpress.org/latest.zip
sudo unzip latest.zip
sudo cp -rf wordpress/* /var/www/html/
sudo chown -R http://x.5izxy.cn/wp-admin/users.phpwww-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo vi /var/www/html/wp-config.php
# 可选 change salt : curl -s https://api.wordpress.org/secret-key/1.1/salt/
/** The name of the database for WordPress */
define('DB_NAME', 'wordpressdb');

/** MySQL database username */
define('DB_USER', 'wpuser');

/** MySQL database password */
define('DB_PASSWORD', 'xxxxxxxx');

/** MySQL hostname */
define('DB_HOST', 'localhost');
sudo vi /etc/apache2/mods-enabled/dir.conf
# 把index.php放于第一个
sudo /etc/init.d/apache2 restart
# 访问并安装
http://<Server’s IP Address>/wp-admin/install.php

#### https ####
sudo apt-get install openssl
sudo a2enmod ssl
sudo vi /etc/apache2/ports.conf  # 查看是否有listen 80; listen 443
sudo ln -s /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-enabled/001-ssl.conf
sudo vi /etc/apache2/sites-enabled/001-ssl.conf
    ServerName x.5izxy.cn
    SSLEngine on
    SSLCertificateFile /home/ubuntu/Apache/2_x.5izxy.cn.crt
    SSLCertificateKeyFile /home/ubuntu/Apache/3_x.5izxy.cn.key
    SSLCertificateChainFile /home/ubuntu/Apache/1_root_bundle.crt

可用资料

UFW Essentials: Common Firewall Rules and Commands
How To Secure Nginx with Let's Encrypt on Ubuntu 16.04
How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 16.04
How To Configure SSL/TLS for MySQL on Ubuntu 16.04
Initial Server Setup with Ubuntu 16.04
How To Set Up Jupyter Notebook for Python 3
GIt
How To Install and Configure Elasticsearch on Ubuntu 16.04
How To Add Swap on Ubuntu 14.04
How To Install Elasticsearch, Logstash, and Kibana (ELK Stack) on Ubuntu 16.04
ELASTICSEARCH CONFIGURATION AND PERFORMANCE TUNING

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,085评论 2 44
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,304评论 5 6
  • (一)女儿准高三时写给爸妈的信 2014.5.25 周日 晚 爸、妈、图图(弟弟): 好久不写信了,再写感觉有点矫...
    纶纶妈阅读 311评论 1 1
  • 孤独礼赞 孤独是黎明前的启明星, 孤独是大海咆哮前的静谧, 孤独是火山爆发前的安宁, 孤独是浩瀚宇宙的一种能量折射...
    明扬天下阅读 670评论 2 2
  • 我爱这个故事。 男主:藤井树 秋叶 女主:藤井树 博子 不知道为什么,我总觉得那些大背景下的大故事有种钝感,既不是...
    菠00阅读 246评论 0 0