cPanel UAPI Operations for Creating Subdomains, MySQL Databases, and File Operations
Tags: cpanel, uapi, iamtakdir
Published: 2024-03-14 19:05:10


**Introduction**

![itwasCpanel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81bxmn2qxctph10a5vur.png)



the cPanel User API (UAPI) stands out as a powerful interface for automating tasks and streamlining administrative operations. In this guide, we will delve into the practical usage of cPanel UAPI for creating subdomains, MySQL databases, and file operations.

[UAPI Documentation](https://api.docs.cpanel.net/cpanel/introduction/)

**Managing Subdomains**

Subdomains are indispensable for organizing and structuring websites within a domain. With cPanel UAPI, creating subdomains becomes a breeze. The following command snippet demonstrates how to create a subdomain:

```bash
uapi SubDomain addsubdomain domain=subdomain domain=example.com rootdomain=example.com dir=public_html/subdomain
```

Simply replace `subdomain` with your desired subdomain name and `example.com` with your domain. Additionally, adjust the directory path (`dir`) as needed to reflect your hosting environment's structure.

**Managing MySQL Databases**

MySQL databases are the backbone of many web applications, storing crucial data and enabling dynamic content generation. Leveraging cPanel UAPI, you can effortlessly create MySQL databases and users with tailored privileges. Let's break down the process into two steps:

1. **Creating a MySQL Database:**

```bash
uapi --user=username Mysql create_database name=mydatabase
```

Replace `username` with your actual username and `mydatabase` with the desired name for your MySQL database.

2. **Creating a MySQL User and Granting Privileges:**

```bash
uapi --user=username Mysql create_user name=myuser password=mypassword
uapi --user=username Mysql set_privileges_on_database user=myuser database=mydatabase privileges=ALL_PRIVILEGES
```

Substitute `username`, `myuser`, `mypassword`, and `mydatabase` with your specific values. These commands will establish a MySQL user with all privileges on the designated database.

**File Operations**

Efficiently managing files and directories is essential for maintaining a well-organized web hosting environment. With cPanel UAPI, you can perform file operations seamlessly. Let's explore two common tasks:

1. **Moving Files:**

```bash
uapi Fileman move_file source_file=public_html/oldfile.txt destination_file=public_html/newfile.txt
```

Replace `source_file` and `destination_file` paths as necessary. This command facilitates the relocation of files within your hosting space.

2. **Moving Directories:**

```bash
uapi Fileman move_dir source_dir=public_html/oldfolder destination_dir=public_html/newfolder
```

Similar to moving files, this command relocates entire directories along with their contents, promoting efficient organization.

**Additional Tips:**

While cPanel UAPI offers robust functionality, certain tasks may be better suited for direct execution via shell commands, especially for bulk operations. For instance, moving or copying files can be accomplished using standard bash commands like `mv` and `cp`:

```bash
# Move all files from source directory to destination directory
mv /path/to/source/* /path/to/destination/

# Copy all files from source directory to destination directory
cp -r /path/to/source/* /path/to/destination/
```

These commands provide flexibility and efficiency, particularly for complex file management tasks.

### Author can talk :3 
In retrospect, I realize there may have been oversights in the documentation review. I apologize for any mistakes . Thank you for your understanding and feedback.