1.准备工作

本教程基于 Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-71-generic x86_64)进行操作
LNMP(mysql5.7+nginx1.22+php7.4)环境部署指导

#安装一些基础包
yum install net-tools vim curl tar

2.配置防火墙

firewall-cmd --list-ports
#显示当前开启端口
firewall-cmd --zone public --add-port 80/tcp  --permanent
firewall-cmd --zone public --add-port 443/tcp  --permanent
firewall-cmd --zone public --add-port 3306/tcp  --permanent
#开启需要使用的端口
systemctl restart firewalld
#重启防火墙

3.安装docker和配置网络

安装参考其它教程

docker network create net0
#创建自定义网络,目的为了容器之间互相通信

3.部署mysql

安装mysql5.7版本

docker pull mysql:5.7

启动mysql

docker run \
--name mysql \
--network net0 \
-d \
-p 3306:3306 \
--restart unless-stopped \
-v /home/mysql/log:/var/log/mysql \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/conf:/etc/mysql/ \
-e MYSQL_ROOT_PASSWORD=密码 \
-e TZ=Asia/Shanghai \
mysql:5.7

配置数据库字符集

[mysqld]
character_set_server=utf8

重启之后验证字符集

show variables like 'character%';

配置数据库用户权限,达到不同的用户只能操作操作不同的数据库名

CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
GRANT ALL PRIVILEGES ON 表名.* TO '用户名'@'%';

4.部署nginx

nginx不能直接启动,需要先准备配置文件nginx.conf,否则会报错,报错提示如下

docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/home/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

docker pull nginx:1.22
mkdir -p /home/nginx/conf
mkdir -p /home/nginx/log
mkdir -p /home/nginx/html
# 创建挂载目录
# 生成容器
docker run --name nginx -p 80:80 -d nginx
docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /home/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /home/nginx/
# 将容器中的文件复制到宿主机
docker ps -a
docker stop nginx
docker rm nginx
# 找到nginx对应的容器id,删除容器
#上面的步骤其实也可以用这个命令代替,强制删除正在运行的容器
docker rm -f nginx

启动nginx

docker run \
--name nginx \
--network net0 \
-p 80:80 -p 443:443 \
--restart unless-stopped \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/nginx/conf/ssl:/etc/nginx/ssl \
-v /home/nginx/log:/var/log/nginx \
-v /home/nginx/html:/usr/share/nginx/html \
-d nginx:1.22

5.部署PHP7.4

docker pull php:7.4-fpm
docker run  \
--name php \
--network net0 \
-p 9000:9000 \
--restart unless-stopped \
-v /home/nginx/html:/www \
-d php:7.4-fpm
#添加数据库相关扩展
sudo docker exec -it php docker-php-ext-install pdo pdo_mysql
sudo docker exec -it php docker-php-ext-install mysqli
解决php容器时区问题
docker exec -it php /bin/bash
echo "Asia/Shanghai" > /etc/timezone
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
printf '[PHP]\ndate.timezone = Asia/Shanghai\n' > /usr/local/etc/php/conf.d/tzone.ini

6.nginx配置文件参考

server {
    listen  80;
    listen 443 ssl http2;
    server_name  hello.runyf.cn;

    location / {
        root   /usr/share/nginx/html/hello;
        index  index.html index.htm index.php;
        
        
        #HTTP_TO_HTTPS_START
        if ($server_port !~ 443){
            rewrite ^(/.*)$ https://$host$1 permanent;
        }
        

    }

    ssl_certificate  /etc/nginx/ssl/hello.runyf.cn_bundle.pem;
    ssl_certificate_key  /etc/nginx/ssl/hello.runyf.cn.key;
    
    
   # ssl验证相关配置
    ssl_session_timeout  5m;    #缓存有效期
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;        

    #加密算法
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全链接可选的加密协议
    ssl_prefer_server_ciphers on;   #使用服务器端的首选算法


    #PHP配置
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/hello/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
最后修改:2024 年 04 月 25 日
如果觉得我的文章对你有用,请随意赞赏