1. 程式人生 > >阿裏雲服務器部署Tornado應用全指南

阿裏雲服務器部署Tornado應用全指南

timestamp 準備 col make where 如果 href 安裝mysql ##

本篇詳細介紹tornado應用部署到阿裏雲服務器上的全過程。

Tornado程序地址:github https://github.com/ddong8/ihasy.git

準備工作:阿裏雲服務器CentOS7.4系統+PuTTY遠程登錄

一.更新CentOS系統

安裝完CentOS7.4後慣例更新下系統:

yum update

  技術分享

二.安裝MySQL

然後安裝MySQL:

卸載MariaDB

CentOS7默認安裝MariaDB而不是MySQL,而且yum服務器上也移除了MySQL相關的軟件包。因為MariaDB和MySQL可能會沖突,故先卸載MariaDB。

1、安裝新版mysql之前,我們需要將系統自帶的mariadb-lib卸載

查找mariadb:

[root@iZwz94qazh62gk5ewl4ei2Z home]# rpm -qa | grep -i mariadb

mariadb-libs-5.5.52-1.el7.x86_64

 rpm -qa | grep -i mariadb

卸載mariadb

[root@iZwz94qazh62gk5ewl4ei2Z home]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64

rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

技術分享

2、到mysql的官網下載最新版mysql的rpm集合包:mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar

然後解壓:

tar -xvf mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar

技術分享

然後安裝:

rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm

rpm -ivh mysql-community-libs-5.7.20-1.el7.x86_64.rpm

rpm -ivh mysql-community-devel-5.7
.20-1.el7.x86_64.rpm

rpm -ivh mysql-community-client-5.7.20-1.el7.x86_64.rpm

安裝mysql-community-server會報錯提示缺少libaio.so.1

技術分享

報錯如上圖,需要安裝libaio.so.1

64位系統安裝:

wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libaio-0.3.107-10.el6.x86_64.rpm

rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm

32位系統現在很少了,yum似乎默認安裝32位的:

yum install libaio.so.1

再安裝即可成功:

rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm

5、 數據庫初始化

為了保證數據庫目錄為與文件的所有者為 mysql 登陸用戶,如果你的linux系統是以 root 身份運行 mysql 服務,需要執行下面的命令初始化

1 [root@iZwz94qazh62gk5ewl4ei2Z mysql]# mysqld --initialize --user=mysql

如果是以 mysql 身份登錄運行,則可以去掉 --user 選項。

另外 --initialize 選項默認以“安全”模式來初始化,則會為 root 用戶生成一個密碼並將該密碼標記為過期,登陸後你需要設置一個新的密碼,

而使用 --initialize-insecure 命令則不使用安全模式,則不會為 root 用戶生成一個密碼。

這裏演示使用的 --initialize 初始化的,會生成一個 root 賬戶密碼,密碼在log文件裏,紅色區域的就是自動生成的密碼

1 2 3 4 5 6 7 [root@iZwz94qazh62gk5ewl4ei2Z mysql]# cat /var/log/mysqld.log 2017-06-05T14:30:52.709474Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-06-05T14:30:55.590590Z 0 [Warning] InnoDB: New log files created, LSN=45790 2017-06-05T14:30:56.000269Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-06-05T14:30:56.109868Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 960c533e-49fb-11e7-91f2-00163e089fd2. 2017-06-05T14:30:56.116186Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened. 2017-06-05T14:30:56.116777Z 1 [Note] A temporary password is generated for root@localhost: :Wu?2QQutQwj

現在啟動mysql數據庫systemctl start mysqld.service(Centos7特有的啟動方式)

可以使用下面兩個命令對mysql進行停止,啟動和重啟:

啟動:

使用 service 啟動:service mysqld start
使用 mysqld 腳本啟動:/etc/inint.d/mysqld start
使用 safe_mysqld 啟動:safe_mysqld&

停止:

使用 service 啟動:service mysqld stop
使用 mysqld 腳本啟動:/etc/inint.d/mysqld stop
mysqladmin shutdown

重啟:

使用 service 啟動:service mysqld restart
使用 mysqld 腳本啟動:/etc/inint.d/mysqld restart

連接數據庫

[root@iZwz94qazh62gk5ewl4ei2Z mysql]# mysql -u root -p
Enter password:

密碼輸入: :Wu?2QQutQwj

修改密碼:

set password = password(‘你的密碼‘);

三.安裝nginx

安裝所需環境

Nginx 是 C語言 開發,建議在 Linux 上運行,當然,也可以安裝 Windows 版本,本篇則使用 CentOS 7 作為安裝環境。

一. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

yum install gcc-c++

二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

yum install -y pcre pcre-devel

三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。

yum install -y zlib zlib-devel

四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。

yum install -y openssl openssl-devel

官網下載

1.直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html

技術分享

2.使用wget命令下載(推薦)。

wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

技術分享

我下載的是1.12.2版本,這個是目前的穩定版。

解壓

依然是直接命令:

tar -zxvf nginx-1.12.2.tar.gz

cd nginx-1.12.2


配置

其實在 nginx-1.12.2 版本中你就不需要去配置相關東西,默認就可以了。當然,如果你要自己配置目錄也是可以的。
1.使用默認配置

./configure

 

編譯安裝

make

make install

 

查找安裝路徑:

whereis nginx

技術分享

啟動、停止nginx

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

./nginx -s quit:此方式停止步驟是待nginx進程處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。

查詢nginx進程:

ps aux|grep nginx

重啟 nginx

1.先停止再啟動(推薦):
對 nginx 進行重啟相當於先停止再啟動,即先執行停止命令再執行啟動命令。如下:

./nginx -s quit
./nginx

2.重新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效需要重啟 nginx,使用-s reload不用先停止 ngin x再啟動 nginx 即可將配置信息在 nginx 中生效,如下:
./nginx -s reload

啟動成功後,在瀏覽器可以看到這樣的頁面:

技術分享

開機自啟動

即在rc.local增加啟動代碼就可以了。

vi /etc/rc.local

增加一行

/usr/local/nginx/sbin/nginx

設置執行權限:

chmod 755 /etc/rc.local

技術分享

到這裏,nginx就安裝完畢了,啟動、停止、重啟操作也都完成了。

反向代理

cd /usr/local/nginx/conf/

使用vi或nano編輯器在該目錄下新建一個ihasy.conf文件輸入以下內容:

vi ihasy.conf

upstream ihasy  {
    server 127.0.0.1:9001; #Tornado
}

## Start www.ihasy.com ##
server {
    listen 80;
    server_name  www.ihasy.com ihasy.com;

    #root   html;
    #index  index.html index.htm index.py index;

    ## send request back to Tornado ##
    location / {
        proxy_pass  http://ihasy;

        #Proxy Settings
        proxy_redirect     off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_max_temp_file_size 0;
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;
        proxy_buffer_size          4k;
        proxy_buffers              4 32k;
        proxy_busy_buffers_size    64k;
        proxy_temp_file_write_size 64k;
   }
}
## End www.ihasy.com ##

再使用vi或nano打開 /usr/local/nginx/conf/nginx.conf

vi /usr/local/nginx/conf/nginx.conf

在http下添加一行

include ihasy.conf;

技術分享

保存,重啟nginx,即可實現反向代理。

四.安裝git

直接通過yum安裝

yum install git

技術分享

獲取tornado應用ihasy.com

git clone https://github.com/ddong8/ihasy.git

進入ihasy目錄

cd ihasy

使用PIP安裝模塊之前,要先安裝 libffi-devel python-devel,不然會報錯

yum install libffi-devel python-devel

技術分享

再使用

pip install -r requirements.txt

安裝模塊完畢

技術分享

  按照下圖創建db,建表

技術分享

五.安裝supervisor

supervisor是用python寫的一個進程管理工具,用來啟動,重啟,關閉進程。

pip install supervisor

supervisor的配置文件
supervisor安裝完畢後,會有一個配置文件supervisord.conf
運行

echo_supervisord_conf

命令,可輸出文件詳細
我們使用重定向運算符將配置文件定向到/etc路徑下(方便管理)

echo_supervisord_conf>/etc/supervisord.conf

在supervisord.conf的末尾添加如下代碼:

vi /etc/supervisord.conf

[program:ihasy]
command=python /root/ihasy/application.py --port=9001 --mysql_database=ihasy --mysql_host=localhost --mysql_password=764895 --mysql_user=root
directory=/root/ihasy
autorestart=true
redirect_stderr=true

supervisor服務啟動
運行命令

supervisord -c /etc/supervisord.conf

supervisor開機啟動

即在rc.local增加啟動代碼就可以了。

vi /etc/rc.local

增加一行:

supervisord -c /etc/supervisord.conf

技術分享

設置執行權限:

chmod 755 /etc/rc.local

然後重啟就可以自動運行nginx,supervisor了。

                                                                                                            

阿裏雲服務器部署Tornado應用全指南