Tóm tắt tính năng:
- Tạo package có tùy chọn Install WordPress
- Tự động cài đặt wordpress khi tạo user (nếu package có bật tùy chọn)
- Tự động cài đặt wordpress khi thêm tên miền, tick chọn cài hoặc không
- Cài wordpress cho tên miền đã thêm vào
- Tự động cài đặt wordpress khi thêm sub domain
Rất hữu ích cho các bạn làm wordpress hosting, làm web demo trên sub domain
Hướng dẫn cài đặt
Để cài đặt chúng ta sẽ login vào SSH và SFTP nhé, công cụ kết nối là bitvise / MobaXterm cho windows, Terminus cho Mac


Miễn phí cài đặt Directadmin + Auto Install WordPress khi mua VPS tại BNIX.VN
VPS NVme – 4 vCPU – 8GB RAM – 65GB NVme chỉ 999k/năm
Bước 1: Cài đặt WP-CLI
Trong SSH chúng ta chạy các lệnh sau để cài đặt WP-CLI
curl-Ohttps://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.pharchmod+xwp-cli.pharsudomvwp-cli.phar/usr/local/bin/wp
Bước 2: Thêm tùy chọn cho package


Để thêm tùy chọn cho package (gói hosting), các bạn chạy lệnh sau:
touch/usr/local/directadmin/data/admin/custom_package_items.confnano/usr/local/directadmin/data/admin/custom_package_items.conf
Tiếp đó điền nội dung sau vào, bấm Ctrl + O và Ctrl + X để save và đóng lại
installWP=type=checkbox&string=InstallWordPress&desc=AutomaticallyinstallWordPresswiththisUserPackage&checked=yes
Phân quyền cho file vừa tạo
chowndiradmin./usr/local/directadmin/data/admin/custom_package_items.confchmod700/usr/local/directadmin/data/admin/custom_package_items.conf
Bước 3: Thêm tùy chọn cho tên miền


Thêm tùy chọn cho tên miền
touch/usr/local/directadmin/data/admin/custom_domain_items.confnano/usr/local/directadmin/data/admin/custom_domain_items.conf
Tiếp đó điền nội dung sau vào, bấm Ctrl + O và Ctrl + X để save và đóng lại
installWP=type=checkbox&string=InstallWordPress&desc=AutomaticallyinstallWordPressforthisdomain&checked=no
Phân quyền cho file cấu hình vừa tạo
chowndiradmin./usr/local/directadmin/data/admin/custom_domain_items.confchmod700/usr/local/directadmin/data/admin/custom_domain_items.conf
Bước 4: Tạo Script cài đặt WordPress
* Script cho bước thêm User:
Chúng ta chạy lệnh sau để tạo file script
touch/usr/local/directadmin/scripts/custom/user_create_post.shnano/usr/local/directadmin/scripts/custom/user_create_post.sh
Sau đó điền mã Script sau vào
#!/bin/bash# CHECK CUSTOM PKG ITEM INSTALLWPif [[ $installWP!='ON' ]]; thenexit0;else# ENABLE ERROR LOGGING# exec 2>/usr/local/directadmin/customscripts.error.log# SET UP DATABASE VARIABLESdbpass=$(openssl rand -base6412)>/dev/nullext=$(openssl rand -hex2)>/dev/nulldbuser="wp${ext}"# do not include the username_ for dataskq here as DA adds thiswpconfigdbuser="${username}_wp${ext}"wpadminpass=$(openssl rand -base6414)>/dev/null# CHECK IF WORDPRESS EXISTSif [ -f /home/$username/domains/$domain/public_html/index.php ]; thenecho"WARNING: There appears to be an index file already located in this directory, which indicates that an installation is already present! Empty the directory before running the script again."exitelse# DISABLE DIRECTADMIN INDEX.HTML FILEif [ -f /home/$username/domains/$domain/public_html/index.html ]; thenmv/home/$username/domains/$domain/public_html/index.html{,.bak}fi# CREATE DATABASE/usr/bin/mysqladmin-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)create ${wpconfigdbuser}# Tạo MySQL user và cấp quyền đầy đủ cho databaseecho"CREATE USER '${wpconfigdbuser}'@'localhost' IDENTIFIED BY '${dbpass}';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)echo"GRANT ALL PRIVILEGES ON ${wpconfigdbuser}.* TO '${wpconfigdbuser}'@'localhost';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)echo"FLUSH PRIVILEGES;"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)# DOWNLOAD WORDPRESScd/home/$username/domains/$domain/public_html/su-s/bin/bash-c"/usr/local/bin/wp core download"$username# SET DATABASE DETAILS IN THE CONFIG FILEsu-s/bin/bash-c"/usr/local/bin/wp config create --dbname=$wpconfigdbuser --dbuser=$wpconfigdbuser --dbpass=$dbpass --dbhost=localhost"$username# INSTALL WORDPRESSsu-s/bin/bash-c"/usr/local/bin/wp core install --url=https://$domain/ --admin_user=$username --admin_password=$wpadminpass --title="$domain" --admin_email=$username@$domain"$usernamesu-s/bin/bash-c"/usr/local/bin/wp rewrite structure '/%postname%/'"$usernameprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn"if [[ !-h /home/$username/domains/$domain/private_html ]]; thenecho"Making a symlink for https..."cd/home/$username/domains/$domain/rm-rfprivate_htmlsu-s/bin/bash-c"ln -s public_html private_html"$usernamefi# ADD LOGIN DETAILS TO TEXT FILEprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn">>/home/$username/domains/$domain/public_html/.wp-details.txtchown$username./home/$username/domains/$domain/public_html/.wp-details.txtfi# DELETE DOLLY PLUGIN AND INSTALL LITESPEED CACHEsu-s/bin/bash-c"/usr/local/bin/wp plugin delete hello"$usernamesu-s/bin/bash-c"/usr/local/bin/wp plugin delete akismet"$username# CREATE .HTACCESScat<<EOF> /home/$username/domains/$domain/public_html/.htaccess# BEGIN WordPressRewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]# END WordPressEOFchown$username./home/$username/domains/$domain/public_html/.htaccess# CHANGE FILE PERMISSIONScd/home/$username/domains/$domain/public_html/find.-typed-execchmod0755{};find.-typef-execchmod0644{};# WORDPRESS SECURITY AND HARDENINGchmod400/home/$username/domains/$domain/public_html/.wp-details.txtchmod400/home/$username/domains/$domain/public_html/wp-config.phpfiexit0;
Tiếp theo là phân quyền cho script
chowndiradmin.diradmin/usr/local/directadmin/scripts/custom/user_create_post.shchmod700/usr/local/directadmin/scripts/custom/user_create_post.sh
* Script Cho bước thêm tên miền (Addon Domain)
Tạo file script
touch/usr/local/directadmin/scripts/custom/domain_create_post.shnano/usr/local/directadmin/scripts/custom/domain_create_post.sh
Thêm nội dung sau cho file script (khá giống với bước add user)
#!/bin/bash# CHECK CUSTOM PKG ITEM INSTALLWPif [[ $installWP!='ON' ]]; thenexit0;else# ENABLE ERROR LOGGING# exec 2>/usr/local/directadmin/customscripts.error.log# SET UP DATABASE VARIABLESdbpass=$(openssl rand -base6412)>/dev/nullext=$(openssl rand -hex2)>/dev/nulldbuser="wp${ext}"# do not include the username_ for dataskq here as DA adds thiswpconfigdbuser="${username}_wp${ext}"wpadminpass=$(openssl rand -base6414)>/dev/null# CHECK IF WORDPRESS EXISTSif [ -f /home/$username/domains/$domain/public_html/index.php ]; thenecho"WARNING: There appears to be an index file already located in this directory, which indicates that an installation is already present! Empty the directory before running the script again."exitelse# DISABLE DIRECTADMIN INDEX.HTML FILEif [ -f /home/$username/domains/$domain/public_html/index.html ]; thenmv/home/$username/domains/$domain/public_html/index.html{,.bak}fi# CREATE DATABASE/usr/bin/mysqladmin-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)create ${wpconfigdbuser}# Tạo MySQL user và cấp quyền đầy đủ cho databaseecho"CREATE USER '${wpconfigdbuser}'@'localhost' IDENTIFIED BY '${dbpass}';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)echo"GRANT ALL PRIVILEGES ON ${wpconfigdbuser}.* TO '${wpconfigdbuser}'@'localhost';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)echo"FLUSH PRIVILEGES;"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)# DOWNLOAD WORDPRESScd/home/$username/domains/$domain/public_html/su-s/bin/bash-c"/usr/local/bin/wp core download"$username# SET DATABASE DETAILS IN THE CONFIG FILEsu-s/bin/bash-c"/usr/local/bin/wp config create --dbname=$wpconfigdbuser --dbuser=$wpconfigdbuser --dbpass=$dbpass --dbhost=localhost"$username# INSTALL WORDPRESSsu-s/bin/bash-c"/usr/local/bin/wp core install --url=https://$domain/ --admin_user=$username --admin_password=$wpadminpass --title="$domain" --admin_email=$username@$domain"$usernamesu-s/bin/bash-c"/usr/local/bin/wp rewrite structure '/%postname%/'"$usernameprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn"if [[ !-h /home/$username/domains/$domain/private_html ]]; thenecho"Making a symlink for https..."cd/home/$username/domains/$domain/rm-rfprivate_htmlsu-s/bin/bash-c"ln -s public_html private_html"$usernamefi# ADD LOGIN DETAILS TO TEXT FILEprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn">>/home/$username/domains/$domain/public_html/.wp-details.txtchown$username./home/$username/domains/$domain/public_html/.wp-details.txtfi# DELETE DOLLY PLUGIN AND INSTALL LITESPEED CACHEsu-s/bin/bash-c"/usr/local/bin/wp plugin delete hello"$usernamesu-s/bin/bash-c"/usr/local/bin/wp plugin delete akismet"$username# CREATE .HTACCESScat<<EOF> /home/$username/domains/$domain/public_html/.htaccess# BEGIN WordPressRewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]# END WordPressEOFchown$username./home/$username/domains/$domain/public_html/.htaccess# CHANGE FILE PERMISSIONScd/home/$username/domains/$domain/public_html/find.-typed-execchmod0755{};find.-typef-execchmod0644{};# WORDPRESS SECURITY AND HARDENINGchmod400/home/$username/domains/$domain/public_html/.wp-details.txtchmod400/home/$username/domains/$domain/public_html/wp-config.phpfiexit0;
Phân quyền cho script
chowndiradmin.diradmin/usr/local/directadmin/scripts/custom/domain_create_post.shchmod700/usr/local/directadmin/scripts/custom/domain_create_post.sh
* Script cho chỉnh sửa tên miền cũ
Có những tên miền cũ đã thêm trước script này hoặc không tick Install ở bước thêm tên miền, giờ chỉ cần Edit tên miền, tick vào tùy chọn Install WordPress rồi Save lại là nó sẽ cài cho chúng ta
Tạo file script
touch/usr/local/directadmin/scripts/custom/domain_modify_post.shnano/usr/local/directadmin/scripts/custom/domain_modify_post.sh
Thêm nội dung sau vào script
#!/bin/bash# CHECK CUSTOM PKG ITEM INSTALLWPif [[ $installWP!='ON' ]]; thenexit0;else# ENABLE ERROR LOGGING# exec 2>/usr/local/directadmin/customscripts.error.log# MOVE CONTENTS TO BACKUP FOLDERmkdir-p/home/$username/wp-backup/$domain-backupmv/home/$username/domains/$domain/public_html/*/home/$username/wp-backup/$domain-backupchown-R$username./home/$username/wp-backup# SET UP DATABASE VARIABLESdbpass=$(openssl rand -base6412)>/dev/nullext=$(openssl rand -hex2)>/dev/nulldbuser="wp${ext}"# do not include the username_ for dataskq here as DA adds thiswpconfigdbuser="${username}_wp${ext}"wpadminpass=$(openssl rand -base6414)>/dev/null# CREATE DATABASE/usr/bin/mysqladmin-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)create ${username}_${dbuser}# Tạo MySQL user và cấp quyền đầy đủ cho databaseecho"CREATE USER '${username}_${dbuser}'@'localhost' IDENTIFIED BY '${dbpass}';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)echo"GRANT ALL PRIVILEGES ON ${username}_${dbuser}.* TO '${username}_${dbuser}'@'localhost';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)echo"FLUSH PRIVILEGES;"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2)# DOWNLOAD WORDPRESScd/home/$username/domains/$domain/public_html/su-s/bin/bash-c"/usr/local/bin/wp core download"$username# SET DATABASE DETAILS IN THE CONFIG FILEsu-s/bin/bash-c"/usr/local/bin/wp config create --dbname=${username}_${dbuser} --dbuser=${username}_${dbuser} --dbpass=$dbpass --dbhost=localhost"$username# INSTALL WORDPRESSsu-s/bin/bash-c"/usr/local/bin/wp core install --url=https://$domain/ --admin_user=$username --admin_password=$wpadminpass --title="$domain" --admin_email=$username@$domain"$usernamesu-s/bin/bash-c"/usr/local/bin/wp rewrite structure '/%postname%/'"$usernameprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn"if [[ !-h /home/$username/domains/$domain/private_html ]]; thenecho"Making a symlink for https..."cd/home/$username/domains/$domain/rm-rfprivate_htmlsu-s/bin/bash-c"ln -s public_html private_html"$usernamefi# ADD LOGIN DETAILS TO TEXT FILEprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn">>/home/$username/domains/$domain/public_html/.wp-details.txtchown$username./home/$username/domains/$domain/public_html/.wp-details.txtfi# Kết thúc khối `if` kiểm tra $installWP# DELETE DOLLY PLUGIN AND INSTALL LITESPEED CACHEsu-s/bin/bash-c"/usr/local/bin/wp plugin delete hello"$usernamesu-s/bin/bash-c"/usr/local/bin/wp plugin delete akismet"$username# CREATE .HTACCESScat<<EOF> /home/$username/domains/$domain/public_html/.htaccess# BEGIN WordPressRewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]# END WordPressEOFchown$username./home/$username/domains/$domain/public_html/.htaccess# CHANGE FILE PERMISSIONScd/home/$username/domains/$domain/public_html/find.-typed-execchmod0755{};find.-typef-execchmod0644{};# WORDPRESS SECURITY AND HARDENINGchmod400/home/$username/domains/$domain/public_html/.wp-details.txtchmod400/home/$username/domains/$domain/public_html/wp-config.phpexit0; # Kết thúc script
Phân quyền cho file script
chowndiradmin.diradmin/usr/local/directadmin/scripts/custom/domain_modify_post.shchmod700/usr/local/directadmin/scripts/custom/domain_modify_post.sh
Lưu ý: Sau khi bạn xác nhận các thay đổi của mình đối với một tên miền hiện có trong DirectAdmin, bạn sẽ nhận được thông báo ở góc dưới bên phải xác nhận rằng tên miền đã được thay đổi. Tuy nhiên, thông tin đăng nhập tài khoản WordPress mới sẽ không được hiển thị trên màn hình như khi thêm tên miền mới. Thay vào đó, chúng sẽ được lưu dưới dạng tệp .wp-details.txt trong public_html thư mục tên miền.
* Script cho sub domain
Cái này áp dụng cho các bạn làm demo bằng sub domain nè.
Tạo file script
touch/usr/local/directadmin/scripts/custom/subdomain_create_post.shnano/usr/local/directadmin/scripts/custom/subdomain_create_post.sh
Thêm nội dung sau cho script
#!/bin/bash# ENABLE ERROR LOGGING# exec 2>/usr/local/directadmin/customscripts.error.log# SET UP DATABASE VARIABLESdbpass=$(openssl rand -base6412)>/dev/nullext=$(openssl rand -hex2)>/dev/nulldbuser="wp${ext}"# do not include the username_ for dataskq here as DA adds thiswpconfigdbuser="${username}_wp${ext}"wpadminpass=$(openssl rand -base6414)>/dev/null# CHECK IF WORDPRESS EXISTSif [ -f /home/$username/domains/$domain/public_html/$subdomain/index.php ]; thenecho"WARNING: There appears to be an index file already located in this directory, which indicates that an installation is already present! Empty the directory before running the script again."exitelse# DISABLE DIRECTADMIN INDEX.HTML FILEif [ -f /home/$username/domains/$domain/public_html/$subdomain/index.html ]; thenmv/home/$username/domains/$domain/public_html/$subdomain/index.html{,.bak}fi# CREATE DATABASE/usr/bin/mysqladmin-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2 )create ${wpconfigdbuser};echo"CREATE USER '${wpconfigdbuser}'@'localhost' IDENTIFIED BY '${dbpass}';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2);echo"GRANT ALL PRIVILEGES ON ${wpconfigdbuser}.* TO '${wpconfigdbuser}'@'localhost';"|mysql-uda_admin-p$(cat /usr/local/directadmin/conf/mysql.conf |grep pass |cut-d=-f2);# DOWNLOAD WORDPRESScd/home/$username/domains/$domain/public_html/$subdomain/su-s/bin/bash-c"/usr/local/bin/wp core download"$username# SET DATABASE DETAILS IN THE CONFIG FILEsu-s/bin/bash-c"/usr/local/bin/wp config create --dbname=$wpconfigdbuser --dbuser=$wpconfigdbuser --dbpass=$dbpass --dbhost=localhost"$username# INSTALL WORDPRESSsu-s/bin/bash-c"/usr/local/bin/wp core install --url=https://$subdomain.$domain/ --admin_user=$username --admin_password=$wpadminpass --title="$subdomain.$domain" --admin_email=$username@$domain"$usernamesu-s/bin/bash-c"/usr/local/bin/wp rewrite structure '/%postname%/'"$usernameprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$subdomain.$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn"# ADD LOGIN DETAILS TO TEXT FILEprintf"nnWORDPRESS LOGIN CREDENTIALS:nURL: https://$subdomain.$domain/wp-admin/nUSERNAME: $usernamenPASSWORD: $wpadminpassnn">>/home/$username/domains/$domain/public_html/$subdomain/.wp-details.txtchown$username./home/$username/domains/$domain/public_html/$subdomain/.wp-details.txt# DELETE DOLLY PLUGIN AND INSTALL LITESPEED CACHEsu-s/bin/bash-c"/usr/local/bin/wp plugin delete hello"$usernamesu-s/bin/bash-c"/usr/local/bin/wp plugin delete akismet"$username# CREATE .HTACCESScat<<EOF> /home/$username/domains/$domain/public_html/$subdomain/.htaccess# BEGIN WordPressRewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]# END WordPressEOFchown$username./home/$username/domains/$domain/public_html/$subdomain/.htaccess# CHANGE FILE PERMISSIONScd/home/$username/domains/$domain/public_html/$subdomain/find.-typed-execchmod0755{};find.-typef-execchmod0644{};# WORDPRESS SECURITY AND HARDENINGchmod400/home/$username/domains/$domain/public_html/$subdomain/.wp-details.txtchmod400/home/$username/domains/$domain/public_html/$subdomain/wp-config.phpfiexit0;
Phân quyền cho file script
chowndiradmin.diradmin/usr/local/directadmin/scripts/custom/subdomain_create_post.shchmod700/usr/local/directadmin/scripts/custom/subdomain_create_post.sh
Lưu ý: File script này áp dụng cho Directadmin bản cũ, subdomain lưu tại thư mục nằm trong public_html. Bản Directadmin có thư mục nằm ở thư mục domains (ngang hàng domain chính) cần phải sửa lại
* Sửa lỗi /bin/bash^M: bad interpreter (nếu có)
Lỗi này sinh ra do trình editor không khớp với shell linux. Sửa bằng cách chạy các lệnh sau:
sed-i's/r$//'/usr/local/directadmin/scripts/custom/domain_create_post.shsed-i's/r$//'/usr/local/directadmin/scripts/custom/subdomain_create_post.shsed-i's/r$//'/usr/local/directadmin/scripts/custom/domain_modify_post.shsed-i's/r$//'/usr/local/directadmin/scripts/custom/user_create_post.sh
Lưu ý chung: Tất cả đều cài dạng https, nên sau khi cài đặt, các bạn phải cài đặt thêm chứng chỉ SSL cho tên miền hoặc tên miền con (subdomain) nhé
Trên là toàn bộ quá trình cài đặt tính năng, các bạn có thể tự cài rồi trải nghiệm
Hướng dẫn sử dụng
Đang cập nhật, nhưng các bạn có thể xem qua Video demo ở trên
Khuyến mãi


Miễn phí cài đặt Directadmin + Auto Install WordPress khi mua VPS tại BNIX.VN
VPS NVme – 4 vCPU – 8GB RAM – 65GB NVme chỉ 999k/năm