Did you know what you can automate process of creation of clients website using cPanel API?
Lets say we have another day at work and need to start few website for our new customers. You can do it manually or automate some action we need to run. For example we could put some files we use every time to create website ( we would surly need to keep them up to date but this is not a big issue as client site will make updates after installation anyway,
So lets create some files in. Add there WP zip file and folder with plugins and themes we often use.
~/public_ftp/
CHANGE THIS DATA SELECT ALL EDIT SAVE
Select each value for variable below and replace them with your data.
siteURLAndDirName = example.pl
dbName = sharedname_examplepl
dbUserName = sharedname_examplepl
password = &0*^jf-YyYs$
cPaneluserName = myusername
COPY FILE SSH
Copy all files from your src catalog to dest folder
cp -R ~/public_ftp/. ~/public_html/example.pl
REMOVE FOLDERS / FILES SSH
If you have any unwanted files you can remove them now. This is simple cleanup. Remove anything you don’t need to save space and keep it clean.
rm ~/public_html/example.pl/wordpress-5.5.1-pl_PL.tar.gz ~/public_html/example.pl/incoming
rm ~/public_html/example.pl/wordpress/wp-content/plugins/*.zip ~/public_html/example.pl/wordpress/wp-content/plugins/hello.php
rm -rf ~/public_html/example.pl/wordpress/wp-content/plugins/akismet
rm ~/public_html/example.pl/wordpress/wp-content/themes/*.zip
rm -rf ~/public_html/example.pl/wordpress/wp-content/themes/twentynineteen ~/public_html/example.pl/wordpress/wp-content/themes/twentyseventeen ~/public_html/example.pl/wordpress/wp-content/themes/twentytwenty ~/public_html/example.pl/wordpress/wp-content/themes/*.zip
———————–MYSQL DATABASE————————————–
CREATE DATABASE USING MYSQL (IN MOST CASES NOT POSSIBLE FOR SSH IN SHARED HOSTING)
To use this method you would have to have database permission to do this stuff. Unfortunately in most cases this will not be a case so we need to find another way. If you are using cPanel then you are in luck! cPanel provides API which can create tables and users from your shared account.
mysql -u myusername -p
----enterPASS---- (zapisane w zeszycie)
# Create database
# CREATE DATABASE sharedname_examplepl;
# Create MYSQL USER
# ...
# Add user to database and setup privilages
# ...
CREATE DATABASE USING CPANEL UAPI (WORKS ON CPANEL SHEARD HSTING)
https://documentation.cpanel.net/display/DD/UAPI+Functions+-+Mysql%3A%3Acreate_database
As you see when you log in to SSH as main user, there will be no need for you to define –user param in the fallowing command. Those commands can help us do almost anything that you could probably do from a cPanel account. From creating database, db users, adding them database specific privileges, adding subdomains and so on see full cPanel UAPI list.
What is missing? Well you will still need to manually add new domains. If you are not on a sherd hosting and you have WHM access you can then turn on and use CPAPI2 API which allows you to park new domains from with in terminal / script.
# https://documentation.cpanel.net/display/DD/UAPI+Functions+-+Mysql%3A%3Acreate_database
# Use Cpanel API to create database // read more here - click "Command Line" toi see example cmd!
# syntax: uapi --user=myusername Mysql create_database name=sharedname_examplepl
# syntax: uapi --user=username Mysql create_user name=dbuser password=12345luggage
# syntax: uapi --user=username Mysql set_privileges_on_database user=dbuser database=mydb privileges=DELETE,UPDATE,CREATE,ALTER
uapi Mysql create_database name=sharedname_examplepl
uapi Mysql create_user name=sharedname_examplepl password="&0*^jf-YyYs$"
uapi Mysql set_privileges_on_database user=sharedname_examplepl database=sharedname_examplepl privileges="ALTER,ALTER ROUTINE,CREATE,CREATE ROUTINE,CREATE TEMPORARY TABLES,CREATE VIEW,DELETE,DROP,EVENT,EXECUTE,INDEX,INSERT,LOCK TABLES,REFERENCES,SELECT,SHOW VIEW,TRIGGER,UPDATE"
You can add those commands by login in to your CPanle account under “terminal” icon or by connecting through SSH to your hosting account using PuTTy.
ADD FTP WITH CPANEL UAPI
# uapi --user=username Ftp add_ftp user=weeones pass=12345luggage quota=42
uapi Ftp add_ftp user=admin1@example.pl pass="&0*^jf-YyYs$" quota=1024
Run bash script instead of running each command by copy and paste.
Also instead of copied and paste we could make more automation by placing it in bash script and running it from cPanel
https://www.youtube.com/watch?v=c0HmVqsynrk
https://www.youtube.com/watch?v=F-gskSl4pwQ
https://forums.cpanel.net/threads/run-bash-script.159246/.
PHP – don’t use shell_exec() in PHP on production machine!.
https://stackoverflow.com/questions/11052162/run-bash-command-from-php