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和配置网络
安装参考其它教程
3.部署mysql
安装mysql5.7版本
docker pull mysql:5.7
创建数据存储目录
mkdir -p ~/mysql/log
mkdir -p ~/mysql/data
mkdir -p ~/mysql/conf/conf.d
mkdir -p ~/mysql/conf/mysql.conf.d
启动mysql
docker run \
--name mysql \
--network net0 \
-d \
-p 3306:3306 \
--restart unless-stopped \
-v ~/mysql/log:/var/log \
-v ~/mysql/data:/var/lib/mysql \
-v ~/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=密码 \
-e TZ=Asia/Shanghai \
mysql:5.7
配置数据库字符集,使用下面这条命令把配置文件复制出来docker cp mysql:/etc/my.cnf ~/mysql/conf
在配置文件中添加如下配置
[mysqld]
character_set_server=utf8
重启之后验证字符集
show variables like 'character%';
配置数据库用户权限,达到不同的用户只能操作操作不同的数据库名
CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
GRANT ALL PRIVILEGES ON 数据库名.* TO '用户名'@'%';
4.部署nginx
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
# 启动容器
docker run --name nginx -p 80:80 -d nginx:1.22
mkdir -p ~/nginx
docker cp nginx:/etc/nginx/nginx.conf ~/nginx/
docker cp nginx:/etc/nginx/conf.d ~/nginx/
docker cp nginx:/usr/share/nginx/html ~/nginx/
# 将容器中的文件复制到宿主机
docker rm -f nginx
启动nginx
docker run \
--name nginx \
--network net0 \
-p 80:80 -p 443:443 \
--restart unless-stopped \
-v ~/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v ~/nginx/conf.d:/etc/nginx/conf.d \
-v ~/nginx/ssl:/etc/nginx/ssl \
-v ~/nginx/log:/var/log/nginx \
-v ~/nginx/html:/usr/share/nginx/html \
-d nginx:1.22
4.2.部署nginx:1.27.1-alpine3.20-perl(推荐)
docker pull nginx:1.27.1-alpine3.20-perl
# 启动容器
docker run --name nginx -p 80:80 -d nginx:1.27.1-alpine3.20-perl
mkdir -p ~/nginx
docker cp nginx:/etc/nginx/nginx.conf ~/nginx/
# 将容器中的文件复制到宿主机
docker rm -f nginx
使用docker-compose启动nginx
services:
nginx:
image: nginx:1.27.1-alpine3.20-perl
container_name: nginx
restart: unless-stopped
network_mode: bridge
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./conf.d:/etc/nginx/conf.d
- ./ssl:/etc/nginx/ssl
- ./log:/var/log/nginx
- ./html:/usr/share/nginx/html
5.部署PHP7.4
docker pull php:7.4-fpm
docker run \
--name php \
--network net0 \
-p 9000:9000 \
--restart unless-stopped \
-v ~/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;
}
}