Ubuntu 安装Mysql+Redis+Nginx

一 、安装MySql

1. 新系统需要安装一下更新

bash 复制代码
apt update -y
apt dist-upgrade -y 
apt install -y wget 

2. 安装mysql,并修改配置
apt install -y mysql-server

mysql配置文件通常位于
vi /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]把bind 改成0.0.0.0,增加连接数,修改mode,开启默认鉴权插件,最后加上编码配置:

复制代码
bind            = 0.0.0.0 #开启远程访问,看实际需要开启
max_connections = 10000  
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION  
default_authentication_plugin = mysql_native_password  
[mysql]  
default-character-set = utf8  

3. 安装成功后重启mysql服务
systemctl restart mysql

4. 初次安装mysql,root账户重置密码(初始的root用户 本地登录是不需要密码的)

sudo mysql -uroot

开始修改密码,因为策略问题,先设置一个符合复杂策略的密码,否则无法做其他操作:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';

查看账户信息:

mysql> use mysql

mysql> select host, user, authentication_string, plugin from user;

5. 创建其他帐号并且外网可以访问:

mysql> CREATE USER 'test'@'%' IDENTIFIED BY 'YourPassword';

mysql> ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'YourPassword';

mysql> grant all privileges on *.* to 'test'@'%';

mysql> flush privileges;

二 、安装Redis

1. 在线安装

apt install -y redis-server

2. 修改 redis.conf 文件,必须开启持久化

vi /etc/redis/redis.conf

复制代码
################################## NETWORK #####################################  
bind 127.0.0.1 -::1   # 将这行代码注释,监听所有的ip地址,外网可以访问  
protected-mode no     # 把yes改成no,允许外网访问  
################################# GENERAL #####################################  
daemonize yes         # 把no改成yes,后台运行  
requirepass YourPassword #去掉前面的井号,并修改密码    
############################## APPEND ONLY MODE ###############################  
appendonly yes        # 把no改成yes,开启持久化  
#appendfsync always  
appendfsync everysec  #设置持久化的方式  
#appendfsync no  

3. 创建 redis 命令软链接
ln -s /usr/bin/redis-cli /usr/bin/redis

通过命令 登录redis测试
redis -a Oatos@123
127.0.0.1:6379> exit
4. 服务操作命令

复制代码
systemctl start redis.service   #启动redis服务  
systemctl stop redis.service   #停止redis服务  
systemctl restart redis.service   #重新启动服务  
systemctl status redis.service   #查看服务当前状态  
systemctl enable redis.service   #设置开机自启动  
systemctl disable redis.service   #停止开机自启动  

三、安装Nginx

1. Ubuntu 安装比较简单:
apt-get install nginx

2. 查找一下安装的目录与查状态

whereis nginx
systemctl status nginx

3. nginx文件安装完成之后的文件位置

/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志

复制代码
service nginx start
service nginx stop
service nginx reload

service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

4. Nginx设置成服务并开机自动启动

systemctl enable nginx

5. 80和443端口相关配置

通过nginx的反省代理访问dotnet服务

vi /etc/nginx/conf.d/myapp.conf

80端口

复制代码
server {  
        listen       80;  
        server_name  xxx.com;  
        proxy_http_version 1.1;
        client_max_body_size    32m;  
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        location / {
            root   /path/web/html;#如果是前后的分离,这里可以填前端的路径,这样子前端就不会有跨域的问题
            index  index.html;
        }  
 location /api {
            proxy_pass http://127.0.0.1:5896; #dotnet服务的地址
        } 
        error_page   500 502 503 504  /50x.html;  
        location = /50x.html {
            root   /usr/share/nginx/html;
        } 
    }  

443端口

复制代码
  server {  
        listen 443 ssl;
        server_name  xxx.com;
        ssl_certificate conf.d/ssl/xxx.com.pem;# 注意该SSL证书路径
        ssl_certificate_key conf.d/ssl/xxx.com.key;#注意该SSL证书路径
        proxy_http_version 1.1;
        client_max_body_size    32m;  
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        location / {
            root   /path/web/html;#如果是前后的分离,这里可以填前端的路径,这样子前端就不会有跨域的问题
            index  index.html;
        }  
 location /api {
            proxy_pass http://127.0.0.1:5896; #dotnet服务的地址
        } 

        error_page   500 502 503 504  /50x.html;  
        location = /50x.html {
            root   /usr/share/nginx/html;
        }  
    } 

6. 测试一下配置

/usr/sbin/nginx -t
/usr/sbin/nginx -s reload 或者 service nginx reload

相关推荐
青花瓷1 小时前
Ubuntu下OpenClaw的安装(豆包火山API版)
运维·服务器·ubuntu
被摘下的星星3 小时前
MySQL count()函数的用法
数据库·mysql
素玥4 小时前
实训5 python连接mysql数据库
数据库·python·mysql
一叶知秋yyds4 小时前
Ubuntu 虚拟机安装 OpenClaw 完整流程
linux·运维·ubuntu·openclaw
喵了几个咪5 小时前
如何在 Superset Docker 容器中安装 MySQL 驱动
mysql·docker·容器·superset
Chasing__Dreams6 小时前
Mysql--基础知识点--95--为什么避免使用长事务
数据库·mysql
风吹迎面入袖凉6 小时前
【Redis】Redis的五种核心数据类型详解
java·redis
a里啊里啊8 小时前
Redis面试题记录
数据库·redis·缓存
数据知道8 小时前
claw-code 源码分析:OmX `$team` / `$ralph`——把 AI 辅助开发从偶发灵感变成可重复流水线
数据库·人工智能·mysql·ai·claude code·claw code
__土块__8 小时前
大厂后端一面模拟:从线程安全到分布式缓存的连环追问
jvm·redis·mysql·spring·java面试·concurrenthashmap·大厂后端