PureFTPd is a popular FTP server that supports various authentication methods, including MySQL. Integrating PureFTPd with MySQL allows you to manage user accounts and permissions directly from a MySQL database. This setup can be particularly useful for web applications where users need to access their files via FTP.
Here's a stepbystep guide on how to set up PureFTPd with MySQL authentication:
Prerequisites
1、PureFTPd Installed: Ensure that PureFTPd is installed on your server. You can download it from the official website or install it using your package manager.
2、MySQL Server Installed: Ensure that MySQL (or MariaDB) is installed and running on your server.
3、PHP Installed: Ensure PHP is installed if you plan to use PHP scripts to interact with the MySQL database.
Step 1: Create a MySQL Database and User Table
First, create a MySQL database and a table to store user credentials.
CREATE DATABASE ftp_users; USE ftp_users; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, user_name VARCHAR(50) NOT NULL, user_pass VARCHAR(50) NOT NULL, home_dir VARCHAR(100) NOT NULL, uid INT NOT NULL, gid INT NOT NULL, shell VARCHAR(50) NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
Insert some sample data into theusers
table:
INSERT INTO users (user_name, user_pass, home_dir, uid, gid, shell) VALUES ('ftpuser', 'password', '/home/ftpuser', 1001, 1001, '/bin/false');
Step 2: Configure PureFTPd to Use MySQL
Edit the PureFTPd configuration file, usually located at/etc/pureftpd/pureftpd.conf
. Add or modify the following lines to enable MySQL authentication:
MYSQLServer 127.0.0.1 MYSQLPort 3306 MYSQLDatabase ftp_users MYSQLUser root MYSQLPassword your_mysql_root_password MYSQLCryptPasswords MYSQLGetPW SELECT user_pass FROM users WHERE user_name='%u' MYSQLCharset utf8
Replaceyour_mysql_root_password
with the actual password for your MySQL root user.
Step 3: Restart PureFTPd
After making these changes, restart the PureFTPd service to apply the new configuration.
sudo systemctl restart pureftpd
Step 4: Testing the Configuration
You can now test the FTP server by connecting using an FTP client like FileZilla or commandline FTP. Use one of the user credentials you inserted into theusers
table.
Step 5: Using PHP to Interact with the MySQL Database (Optional)
If you want to manage users programmatically using PHP, you can connect to the MySQL database and perform CRUD operations. Here’s a simple example of how to connect to the database and retrieve user information:
<?php $servername = "127.0.0.1"; $username = "root"; $password = "your_mysql_root_password"; $dbname = "ftp_users"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn>connect_error) { die("Connection failed: " . $conn>connect_error); } $sql = "SELECT id, user_name, home_dir FROM users"; $result = $conn>query($sql); if ($result>num_rows > 0) { // Output data of each row while($row = $result>fetch_assoc()) { echo "ID: " . $row["id"]. " Name: " . $row["user_name"]. " Home Dir: " . $row["home_dir"]. "<br>"; } } else { echo "0 results"; } $conn>close(); ?>
This script will connect to the MySQL database and print out the list of users stored in theusers
table.
By following these steps, you should have a working PureFTPd server configured to authenticate users against a MySQL database.