用VPS快速部署网站,docker-compose一键部署(Nginx+Mysql+PHP)

多网站独立NGINX配置

PHP双环境部署

所有网站对接Redis加速

docker-compose一键部署(Nginx+Mysql+PHP)

本文6大特点:

1.nginx站点独立配置,更灵活,方便管理,更简单

2.不必要端口映射隐藏,提升安全性。

3.PHP双环境部署,对应多类型网站源码适用,兼容性更强!

 (展示WordPress,苹果CMS,独角数卡共存!)

4.所有类型的网站接入Redis,速度更快!

5.每个网站部署独立自己数据库,更好管理,提升性能与安全!

6.支持ARM机型的搭建,更强兼容性!

更新系统

apt update -y  && apt upgrade -y && apt install -y curl wget sudo socat unzip tar htop

安装docker

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

创建目录

cd /home

mkdir web

cd web

mkdir html

mkdir mysql

mkdir certs

mkdir conf.d

mkdir redis

nano docker-compose.yml

配置docker-compose

https://github.com/kejilion/docker/blob/main/LNMP-docker-compose-4.yml

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

  php74:
    image: php:7.4.33-fpm
    container_name: php74
    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: webroot
      MYSQL_USER: CatName
      MYSQL_PASSWORD: CatPassword

  redis:
    image: redis
    container_name: redis
    restart: always
    volumes:
      - ./redis:/data

生产环境下数据库MySQL版本最好固定(例如8.1.0),避免突然变更不兼容版本导致数据丢失。

升级数据库前需要备份数据。

更新补充:nginx生成的nginx.conf文件里会自动“include”(包含) conf.d文件夹下的所有*.conf文件到http block 里面,具体示例如下:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # 看这里,自动包含了
    include /etc/nginx/conf.d/*.conf;
}

这意味着,我们可以直接通过修改挂载文件,即conf.d文件夹下的文件,就可以启用gzip加速(可以加速网页加载速度约52%,参考文章)。


有些VPS没开端口,先开放端口

iptables -P INPUT ACCEPT

iptables -P FORWARD ACCEPT

iptables -P OUTPUT ACCEPT

iptables -F

证书申请

curl https://get.acme.sh | sh
~/.acme.sh/acme.sh --register-account -m [email protected] --issue -d web1.xxxx.com  -d web2.xxxx.com  -d web3.xxxx.com -d web4.xxxx.com -d web5.xxxx.com -d web6.xxxx.com --standalone --key-file /home/web/certs/key.pem --cert-file /home/web/certs/cert.pem  --force

(小技巧:也可以使用*.xxxx.com解析全部的地址)

nginx站点配置

https://github.com/kejilion/nginx/blob/main/yuming.com.conf

{url}.conf 配置文件如下:

server {
    listen 80;
    server_name yuming.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name yuming.com;

    http2 on;
        
    ssl_certificate /etc/nginx/certs/cert.pem;
    ssl_certificate_key /etc/nginx/certs/key.pem;

    root /var/www/html/web1/wordpress;
    # root /var/www/html/web2/;
    # root /var/www/html/web3/upload/;      
    # root /var/www/html/web4/maccms10-master/;    
    # root /var/www/html/web5//dujiaoka/public/;
    # root /var/www/html/web6/;
    index index.php;

    try_files $uri $uri/ /index.php?$args;

    # 苹果cms伪静态
    # rewrite ^/index.php(.*)$ /index.php?s=$1 last;   
    # rewrite ^/vip.php(.*)$ /vip.php?s=$1 last;    
    # rewrite ^/api.php(.*)$ /api.php?s=$1 last; 

    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;  
}

下载源码

wordpress

cd /home/web/html/ && mkdir web1 && cd web1 &&  wget https://cn.wordpress.org/wordpress-6.2.2-zh_CN.zip && unzip wordpress-6.2.2-zh_CN.zip && rm wordpress-6.2.2-zh_CN.zip

可道云网盘

cd /home/web/html/ && mkdir web2 && cd web2 &&  wget https://static.kodcloud.com/update/download/kodbox.1.42.zip && unzip kodbox.1.42.zip && rm kodbox.1.42.zip

discuz论坛/迪库兹论坛

cd /home/web/html/ && mkdir web3 && cd web3 && wget https://github.com/kejilion/Website_source_code/raw/main/Discuz_X3.5_SC_UTF8_20230520.zip && unzip Discuz_X3.5_SC_UTF8_20230520.zip && rm Discuz_X3.5_SC_UTF8_20230520.zip

苹果cms

cd /home/web/html && mkdir web4 && cd web4 && wget https://github.com/magicblack/maccms_down/raw/master/maccms10.zip && unzip maccms10.zip && rm maccms10.zip

cd /home/web/html/web4/maccms10-master/template/ && wget https://github.com/kejilion/Website_source_code/raw/main/DYXS2.zip && unzip DYXS2.zip && rm /home/web/html/web4/maccms10-master/template/DYXS2.zip 

cp /home/web/html/web4/maccms10-master/template/DYXS2/asset/admin/Dyxs2.php /home/web/html/web4/maccms10-master/application/admin/controller 

cp /home/web/html/web4/maccms10-master/template/DYXS2/asset/admin/dycms.html /home/web/html/web4/maccms10-master/application/admin/view/system

mv /home/web/html/web4/maccms10-master/admin.php /home/web/html/web4/maccms10-master/vip.php && wget -O /home/web/html/web4/maccms10-master/application/extra/maccms.php https://raw.githubusercontent.com/kejilion/Website_source_code/main/maccms.php

独角数卡

cd /home/web/html && mkdir web5 && cd web5 && wget https://github.com/assimon/dujiaoka/releases/download/2.0.6/2.0.6-antibody.tar.gz && tar -zxvf 2.0.6-antibody.tar.gz && rm 2.0.6-antibody.tar.gz

Joomla4/乔姆拉4

cd /home/web/html && mkdir web6 && cd web6 && wget https://downloads.joomla.org/cms/joomla4/4-3-3/Joomla_4-3-3-Stable-Full_Package.zip && unzip Joomla_4-3-3-Stable-Full_Package.zip && rm Joomla_4-3-3-Stable-Full_Package.zip

一键运行

cd /home/web && docker-compose up -d

赋予权限

docker exec -it nginx chmod -R 777 /var/www/html

docker exec -it php chmod -R 777 /var/www/html

docker exec -it php74 chmod -R 777 /var/www/html

安装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’

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’

php7.4安装PHP扩展,调整上传文件大小限制,内存限制

docker exec php74 apt update && docker exec php74 apt install -y libmariadb-dev-compat libmariadb-dev libzip-dev libmagickwand-dev imagemagick

docker exec php74 docker-php-ext-install mysqli pdo_mysql zip gd intl bcmath opcache

docker exec php74 pecl install imagick && docker exec php74 sh -c ‘echo “extension=imagick.so” > /usr/local/etc/php/conf.d/imagick.ini’

docker exec php74 pecl install redis && docker exec php74 sh -c ‘echo “extension=redis.so” > /usr/local/etc/php/conf.d/docker-php-ext-redis.ini’

docker exec php74 sh -c ‘echo “upload_max_filesize=50M \n post_max_size=50M” > /usr/local/etc/php/conf.d/uploads.ini’

docker exec php74 sh -c ‘echo “memory_limit=256M” > /usr/local/etc/php/conf.d/memory.ini’

重启php 

docker restart php

docker restart php74

查看php扩展安装情况

docker exec -it php php -m

docker exec -it php74 php -m

创建多个新数据库

docker exec -it mysql mysql -u root -p

CREATE DATABASE web1;

CREATE DATABASE web2;

CREATE DATABASE web3;

CREATE DATABASE web4;

CREATE DATABASE web5;

CREATE DATABASE web6;

查看数据库列表

SHOW DATABASES;

数据库赋予权限

GRANT ALL PRIVILEGES ON web1.* TO ‘kejilion’@’%’;

GRANT ALL PRIVILEGES ON web2.* TO ‘kejilion’@’%’;

GRANT ALL PRIVILEGES ON web3.* TO ‘kejilion’@’%’;

GRANT ALL PRIVILEGES ON web4.* TO ‘kejilion’@’%’;

GRANT ALL PRIVILEGES ON web5.* TO ‘kejilion’@’%’;

GRANT ALL PRIVILEGES ON web6.* TO ‘kejilion’@’%’;

查看权限赋予情况

SHOW GRANTS FOR ‘kejilion’@’%’;

删除数据库

DROP DATABASE web3;

REVOKE ALL PRIVILEGES ON web3.* FROM ‘kejilion’@’%’;

wordpress后期配置(登录wordpress后,再于vps上输入这个命令,用于免ftp删除插件)

echo “define(‘FS_METHOD’, ‘direct’); define(‘WP_REDIS_HOST’, ‘redis’); define(‘WP_REDIS_PORT’, ‘6379’);” >> /home/web/html/web1/wordpress/wp-config.php

苹果CMS后期配置

后台自定义菜单管理主题

电影先生2.0,/vip.php/admin/Dyxs2/dyxs2set

定时任务

crontab -e

30 * * * * wget –no-check-certificate -O /dev/null https://mac.kejilion.eu.org/api.php/timming/index.html?enforce=1&name=aa

独角数卡后期配置

如果登录时报错再使用该命令,解除HTTPS限制

sed -i ‘s/ADMIN_HTTPS=false/ADMIN_HTTPS=true/g’ /home/web/html/web5/dujiaoka/.env

redis日志查看命令

docker exec -it redis redis-cli monitor

(原始文章从kejilion转载并补充改进大量细节,包括数据库版本控制,niginx启用gzip加速等等,26.09.2023)

分享你的喜爱

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注