个人云服务器简单配置

本地软件

  • 远程终端:putty
  • SFTP文件传输:Filezilla,端口22

安装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 "

Hello There!

" 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
  • celery系统服务可选
# 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
  • JVM heap size
# 启动
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'
  • elastic-api
# 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'
  • sample data

PostgreSQL

  • Setting up PostgreSQL with Python 3 and psycopg on Ubuntu 16.04
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

  • How To Install Wordpress on Ubuntu 14.04 - 安全系数高

  • How to Install WordPress in Ubuntu 14.10 - 较简易

# 需要将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  entry add # 改掉域名
ServerName x.5izxy.cn

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

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:///wp-admin/install.php

  • Package 'php5-gd' has no installation candidate

  • 增加https ( 1. 腾讯云证书安装指引 2.ubuntu下apache配置https且强制http转向为https)

#### 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
  • wp rest api 插件 问题 Rest API shows “Not Found” -> https://x.5izxy.cn/?rest_route=/wp/v2/posts&per_page=1&page=1

  • 使用WordPress的子主题功能修改你的WordPress主题

可用资料

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

你可能感兴趣的:(个人云服务器简单配置)