基于Ubuntu18.04一站式部署(python-mysql-redis-nginx)
阅读原文时间:2021年09月13日阅读:1

1. 安装依赖

~$ sudo apt install openssl* zlib*

2. 安装python3.6.8(个人建议从官网下载后上传到服务器)

点击下载python3.6.8官网源码 或根据下面的命令安装

~$ sudo wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz

3. 解压.tgz文件 并 进入Python-3.6.8文件夹

~$ sudo tar -xzvf Python-3.6.8.tgz

~$ cd Python-3.6.8

4. 将python安装到目录下

~$ sudo ./configure --prefix=/usr/local/python --with-ssl --enable-optimizations

5. 编译并且安装

~$ sudo make && sudo make install

6. 安装完成之后 可以删除源码压缩包与解压出来的文件夹了

~$ rm -rf Python-3.6.8.tgz Python-3.6.8

7. 创建python3软链接

~$ sudo ln -s /usr/local/python/bin/python3.6 /usr/bin/python
# 注意 报错信息:
ln: failed to create symbolic link '/usr/bin/python': File exists

7.1 解决方法与查看指向

1. 查看Linux软连接存放位置
~$ cd /usr/bin
2. 查看现在的python软连接指向的版本
~$ ls -al python
3. 解决方法:
~$ sudo ln -sf   /usr/python/bin/python3  /usr/bin/python

8. 创建pip3的软链接

sudo ln -s /usr/local/python/bin/pip3.6 /usr/bin/pip

验证安装是否成功 输入python3 回车即可

~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
~$

1. 根据上述python的安装 已经实现安装pip3 安装依赖

# 查看pip3 是否成功执行
~$ pip3 list

# 安装依赖
~$ pip3 install virtualenv
~$ pip3 install virtualenvwrapper

2. 配置文件的修改

# 查看配置文件是否存在 .bashrc
~$ ls -lah .bashrc

# 在文件最后面添加 如果没有权限 请使用sudo vim ~/.bashrc
~$ vim ~/.bashrc

export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv
source ~/.local/bin/virtualenvwrapper.sh

# 退出编辑 并保存
先按ESC键,再输入:wq 

# 保存成功后 重启配置文件 (注意 需要重启配置文件 才会生效)
~$ source ~/.bashrc

3. 虚拟环境的操作(增 删 查 退出)

#1. 创建虚拟环境
~$ mkvirtualenv xxx

#2. 删除虚拟环境
~$ rmvirtualenv xxx

#3. 查看虚拟环境
~$ workon # 之所以能用workon 是因为刚刚的.bashrc文件里面配置了

#4. 退出虚拟环境(在使用的时候)
~$ deactivate

1. MySQL的安装命令

~$ sudo apt-get install mysql-server

2. 安装完成后 测试连接

# 因为安装的过程中没让设置密码,进不去mysql。
~$ mysql -uroot -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

3. 修改配置文件(mysqld.cnf) 每次修改配置文件都重启

# /etc/mysql/mysql.conf.d/mysqld.cnf MySQL的配置文件
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 在文件最后面 添加一句 不启动grant-tables授权表 作用可以不用密码登录进去mysql。
skip-grant-tables

# 退出编辑 并保存
先按ESC键,再输入:wq

4. 重启MySQL服务 需要服务登录密码验证

~$ service mysql restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: ubuntu,,, (ubuntu)
Password: 

5. 设置密码 并重启服务

# 此时是可以不用密码登录mysql的
~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.35-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql; # 使用某库
Reading table ...
Database changed # 成功的提示

mysql> update user set authentication_string=password("pwd") where user="root"; # 修改root用户的密码
Query OK ... # 成功的提示

mysql> flush privileges; # 刷新权限
Query OK ... # 成功的提示

mysql> exit # 退出mysql

# 将刚刚的配置文件里面的skip-grant-tables注释掉
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# skip-grant-tables

# 重启MySQL服务 以确保修改的配置文件生效
~$ service mysql restart

6. 如果此时登录mysql还是失败(下面有些命令可在上述中查到)

# 此时如果登录mysql还是失败的话 请执行以下步骤
~$ mysql -uroot -p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

#1. 将配置文件的skip-grant-tables注释打开 sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#2. 重启MySQL服务 service mysql restart
#3. 此时是不需要密码登录mysql的 mysql -uroot -p
#4. use mysql;

#5. select user, plugin from user; # 查看user表的user,plugin字段
mysql> select user,plugin from user;
+------------------+-----------------------+
| user             | plugin                |
+------------------+-----------------------+
| root             | auth_socket            |
| mysql.session    | mysql_native_password |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+

#6. root对应的plugin为auth_socket 这也是登录不了的原因 修改plugin值
mysql> update user set authentication_string=password("pwd"),plugin='mysql_native_password' where user='root';

#7. 再次输入查看user表
mysql> select user,plugin from user;
+------------------+-----------------------+
| user             | plugin                |
+------------------+-----------------------+
| root             | mysql_native_password |
| mysql.session    | mysql_native_password |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+

#8. 修改成功 退出mysql exit
#9. 修改配置文件 将skip-grant-tables注释 sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#10. 重启MySQL服务 service mysql restart

7. 连接mysql成功 并设置外部可连接(注意安全组开放3306端口)

#1. 此时登录mysql 就要使用设置的密码登录了
~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
...
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '×××' WITH GRANT OPTION; # 命令中的 % 代表所有ip地址 xxx:你的密码

mysql> flush privileges; # 刷新权限

mysql> exit # 退出mysql

#2. 找到配置文件 将bing注释掉
~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# bind=127.0.0.1 注释掉

1. 安装redis

~$ sudo apt-get install redis-server

2. 查看redis的进程

~$ ps -ef|grep redis

3. 修改配置文件(需要root权限)

~$ sudo vim /etc/redis/redis.conf

4. 在bind下添加密码设置(并注释bind)

# bind 127.0.0.1 ::1 # 注释bind 目的为了外部可连接
requirepass pwd # 设置密码 外部连接需要通过密码连接

5. redis的启动/重启

~$ service redis start/restart

6. 查看端口(6379)

~$ netstat -nlt|grep 6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN
tcp6       0      0 :::6379                 :::*                    LISTEN

1. 安装nginx

~$ sudo apt-get install nginx

2. 检查安装是否成功

~$ nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

3. nginx的启动/重启

~$ service nginx start/restart

4. 查看进程

~$ ps -ef|grep nginx

5. 查看nginx的相关文件储存位置

~$ whereis nginx

6. 相关文件的介绍

/usr/sbin/nginx:主程序
/usr/lib/nginx: 模块依赖库文件
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志