Linux Caddy Mariadb PHP Wordpress

Có khá nhiều bộ công cụ giúp bạn tự cài đặt WordPress chỉ bằng 1 dòng lệnh 😀

Webinoly, Centmin Mod, CyberPanel, aaPanel, Slick­Stack, OneinStack, EasyEngine, RunCloud hay WPTangToc của Việt Nam

Phần lớn chúng đều được chạy trên Webserver là Nginx hoặc LiteSpeed/ Open LiteSpeed, có điều trên haproxy.tech, mình muốn nó cá nhân hơn một chút nên quyết định tự cài đặt Linux-Caddy-MariaDB-PHP theo hướng dẫn của 秋水逸冰 (teddysun)

Về mặt lý thuyết bạn có thể dùng bất cứ hệ điều hành nào tùy thích, mình dùng AlmaLinux 9 trên VPS Oracle

  • Tạo tài khoản root
sudo passwd root

(Điền mật khẩu và enter) – làm 2 lần để đổi password cho root

  • Tiếp theo gõ
sudo dnf install nano -y
sudo nano /etc/ssh/sshd_config

  Thêm vào 3 dòng

PermitRootLogin yes
PasswordAuthentication yes
ClientAliveInterval 7200

Chú ý xóa các dòng có cùng nội dung mà đang để giá trị no, nhớ xóa đi

Ctrl+O -> Enter -> Ctrl+X để save và exit. Gõ tiếp lệnh sau để khởi động lại ssh:

sudo service sshd restart

Lúc này đã có thể login với tài khoản root

Cài đặt LCMP 

sudo dnf install wget -y
sudo wget https://go.bibica.net/lcmp -O lcmp.sh && sudo chmod +x lcmp.sh && sudo ./lcmp.sh

Sau khi cài đặt xong sẽ thấy info root password và 1 database tạo sẵn mặc định

Database Root Password: Thisisdbrootpassword
Database Name: wordpress_database_name_99999
Database User: wordpress_user_99999
Database Pass: password_pass_99999

Có thể reboot lại 1 phát cho sạch sẽ hệ thống

Cài đặt WordPress cho haproxy.tech

Tạo file cấu hình Caddy + SSL cho haproxy.tech

sudo wget --no-check-certificate https://raw.githubusercontent.com/bibicadotnet/haproxy/main/haproxy.tech.pem -O /etc/ssl/haproxy.tech.pem
sudo wget --no-check-certificate https://raw.githubusercontent.com/bibicadotnet/haproxy/main/haproxy.tech.key -O /etc/ssl/haproxy.tech.key
nano /etc/caddy/conf.d/haproxy.tech.conf
  • Điền vào nội dung bên dưới
www.haproxy.tech {
    redir https://haproxy.tech{uri}
}
haproxy.tech {

# Set this path to your site's directory.
    root * /home/haproxy/htdocs/haproxy.tech
# ssl cloudflare
    tls /etc/ssl/haproxy.tech.pem /etc/ssl/haproxy.tech.key

     encode zstd gzip

    @disallowed {
       path /xmlrpc.php
        path /wp-content/uploads/*.php
    }

    rewrite @disallowed /index.php

    # Serve a PHP site through php-fpm
    php_fastcgi unix//run/php-fpm/www.sock

    
    @static {
        file
        path *.css *.js *.ico *.woff *.woff2
    }
    handle @static {
        header Cache-Control "public, max-age=31536000"
    }

    @static-img {
        file
        path *.gif *.jpg *.jpeg *.png *.svg *.webp *.avif
    }
    handle @static-img {
        header Cache-Control "public, max-age=31536000, immutable"
    }


    # Enable the static file server.
    file_server {
    precompressed zstd gzip
        index index.html
    }


    log {
        output file /var/log/caddy/ssl_access.log {
            roll_size 100mb
            roll_keep 3
            roll_keep_for 7d
        }
    }
}
  • Khởi động lại Caddy
systemctl restart caddy

Cấu hình reverse proxy cho Jetpack Photon

nano /etc/caddy/conf.d/api.haproxy.tech.conf
  • Điền vào nội dung bên dưới
api.haproxy.tech {

    tls /etc/ssl/haproxy.tech.pem /etc/ssl/haproxy.tech.key
    

    handle_path /* {
    http_cache {
    	cache_type in_memory
#        cache_type file 
#        path /var/www/bibica.net/cache
        match_path /
        default_max_age 3153600000s
    status_header api.haproxy.tech
    }
    rewrite * /haproxy_tech/wp-content/uploads/{uri}
    reverse_proxy https://i0.wp.com {
    header_up Host {upstream_hostport}
    }
    }
}

Dòng 15, sửa /haproxy_tech/ thành /haproxy.tech/ (domain của bạn)

Cài đặt Wordpress

mkdir -p /home/haproxy/htdocs/haproxy.tech
cd /home/haproxy/htdocs/haproxy.tech
wp core download --allow-root
wp core config --dbhost=localhost --dbname=wordpress_database_name_99999 --dbuser=wordpress_user_99999 --dbpass=password_pass_99999 --allow-root
chown -R caddy:caddy /home/haproxy/htdocs/haproxy.tech
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

Lúc này bạn có thể gõ domain haproxy.tech vào trình duyệt để cài đặt tài khoản admin 😀

Update: đây là bản cập nhập sau gần 4 tháng biết tới Caddy, mình bổ xung addon Caddy Http Cache để cache cho các ảnh từ Jetpack Photon

2 thoughts on “Linux Caddy Mariadb PHP Wordpress”

Leave a Comment