1. 配置环境
首先更新服务器系统以及安装常用软件
apt update -y && apt upgrade -y && apt install -y curl wget sudo socat unzip tar htop
安装docker 和 docker-compose
curl -fsSL https://get.docker.com | sh curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose
创建目录
mkdir -p /home/web/{html,mysql,certs,conf.d,redis}
2. 创建docker-compose.yml文件
cd /home/web nano docker-compose.yml
把以下代码复制进去:
version: '3.8' services: nginx: image: nginx container_name: nginx restart: always ports: - "80:80" - "443:443" volumes: - ./conf.d:/etc/nginx/conf.d - ./certs:/etc/nginx/certs - ./html:/var/www/html - ./log/nginx:/var/log/nginx php: image: php:fpm container_name: php restart: always volumes: - ./html:/var/www/html mysql: image: mysql:8.1.0 container_name: mysql restart: always volumes: - ./mysql:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: zhumaoroot MYSQL_USER: zhumao MYSQL_PASSWORD: zhumaopass redis: image: redis container_name: redis restart: always volumes: - ./redis:/data
开放VPS上的端口:(如果你的服务器有安全组,比如GCP,AWS,甲骨文,记得还要去面板里开放端口)
iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F
3. 给网站申请证书:
curl https://get.acme.sh | sh
以下内容换成自己的:
- [email protected] –> 你的邮箱
- WangZhan.com –> 你的域名
~/.acme.sh/acme.sh --register-account -m [email protected] --issue -d WangZhan.com --standalone --key-file /home/web/certs/key.pem --cert-file /home/web/certs/cert.pem --force
4. nginx配置:
include /etc/nginx/conf.d/*.conf;
cd /etc/nginx/conf.d/ nano {你的域名}.conf
然后把以下代码复制进去:
server { listen 80; server_name WangZhan.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name WangZhan.com; http2 on; ssl_certificate /etc/nginx/certs/cert.pem; ssl_certificate_key /etc/nginx/certs/key.pem; root /var/www/html/web/wordpress; index index.php; try_files $uri $uri/ /index.php?$args; location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } client_max_body_size 50m; }
5. 下载wordpress
下载最新wordpress到web1文件下,方便以后搭建其他网站web2 web3等等:
cd /home/web/html/ && mkdir web1 && cd web1 && wget https://cn.wordpress.org/latest-zh_CN.zip && unzip wordpress-*.zip && rm wordpress-*.zip
6. 运行docker-compose, 把网页跑起来!
cd /home/web && docker-compose up -d
赋予权限
docker exec -it nginx chmod -R 755 /var/www/html docker exec -it php chmod -R 755 /var/www/html docker exec -it php74 chmod -R 755 /var/www/html
安装PHP扩展
安装PHP扩展 docker exec php apt update && docker exec php apt install -y libmariadb-dev-compat libmariadb-dev libzip-dev libmagickwand-dev imagemagick docker exec php docker-php-ext-install mysqli pdo_mysql zip exif gd intl bcmath opcache docker exec php pecl install imagick && docker exec php sh -c 'echo "extension=imagick.so" > /usr/local/etc/php/conf.d/imagick.ini' docker exec php pecl install redis && docker exec php sh -c 'echo "extension=redis.so" > /usr/local/etc/php/conf.d/docker-php-ext-redis.ini'
PHP上传文件大小限制,内存限制,根据需要调整
docker exec php sh -c 'echo "upload_max_filesize=50M \n post_max_size=50M" > /usr/local/etc/php/conf.d/uploads.ini' docker exec php sh -c 'echo "memory_limit=256M" > /usr/local/etc/php/conf.d/memory.ini'
耐心等待上面步骤完成后,重启php
docker restart php
查看php扩展安装情况
docker exec -it php php -m
创建多个新数据库
docker exec -it mysql mysql -u root -p CREATE DATABASE web1;
数据库赋予权限,注意用户名和docker-compose.yml文件里的用户名匹配
GRANT ALL PRIVILEGES ON web1.* TO 'zhumao'@'%';
7. 登录你的网站,完成wordpress设置
完成后在服务器输入以下命令,设置redis
echo "define('FS_METHOD', 'direct'); define('WP_REDIS_HOST', 'redis'); define('WP_REDIS_PORT', '6379');" >> /home/web/html/web1/wordpress/wp-config.php
附录(更多命令供参考):
数据库mysql容器内:
查看数据库
SHOW DATABASES;
查看权限赋予情况
SHOW GRANTS FOR 'zhumao'@'%';
删除数据库
DROP DATABASE web6; REVOKE ALL PRIVILEGES ON web6.* FROM 'zhumao'@'%';
参考: