
Using Vultr Object Storage can give flexibility and cloud storage that allows applications greater flexibility and access worldwide. Vultr Object Storage is compatible with a subset of the S3 API. See our compatibility matrix for details. In this guide you'll learn how to combine this technology with PHP and create a user portal that allows uploads directly to Vultr's Object Storage.
This tutorial walks through building a web site, using Nginx, allowing direct image uploads to Vultr Object Storage using PHP and JavaScript.
For this install, add two repositories to ensure the latest versions of PHP and Nginx. To do that, add two repositories from one of the Ubuntu developers:
After adding the repositories, update apt, install PHP, unzip, nginx, and Composer:
To add extra security, add an upload user to the system. This runs the PHP FastCGI Process Manager (PHP-FPM) as well as the permissions for the web site and log files.
To support file uploads, change the php.ini settings that the PHP-FPM CGI process uses. Edit /etc/php/8.0/fpm/php.ini and change upload_max_filesize = 2M to upload_max_filesize = 10M. Next, change post_max_size = 8M to be post_max_size = 10M. These two values should match and is the largest file upload size. Save the file and exit.
Make a copy of the default www.conf, which runs the PHP-FPM Pool:
Now rename the original file, essentially disabling it:
The extension .dist is commonly used to denote the default file in the distribution. Furthermore, by naming it something other than .conf ensures the interpreter does't read the file.
Now open /etc/php/8.0/fpm/pool.d/upload.conf and make the following changes:
[www] to [upload]user = www-data to user = uploadgroup = www-data to group = uploadlisten = /run/php/php8.0-fpm.sock to listen = /run/php/php8.0-fpm-upload.sockpm = dynamic to pm = ondemandpm.max_children = 5 to pm.max_children = 22 (NOTE: This value is dependent on the total RAM, Reserved Memory, Buffer and Process size. This calculator can help set the exact value.)pm.start_servers = 2 to pm.start_servers = 5pm.min_spare_servers = 2 to pm.min_spare_servers = 5pm.max_spare_servers = 3 to pm.max_spare_servers = 16;pm.process_idle_timeout = 10s; to pm.process_idle_timeout = 10s; (remove the comma at the front)Save the file and exit. Restart PHP-FPM by running sudo service php8.0-fpm restart. PHP-FPM uses the new values now.
To help secure Nginx, add a snippets.d directory with more configurations that the web server accesses.
After creating the directory, create the support files with the following content:
# nano /etc/nginx/snippets.d/deny-git.conf
# nano /etc/nginx/snippets.d/deny-composer.conf
# nano /etc/nginx/snippets.d/deny-htaccess.conf
# nano /etc/nginx/snippets.d/deny-env.conf
# nano /etc/nginx/snippets.d/deny-license-readme.conf
# nano /etc/nginx/snippets.d/add-headers.conf
To secure Nginx, create two new sites. Using an editor, create /etc/nginx/sites-available/no-default with the following:
This prevents requests to the server that don't match the fully qualified domain.
Create another file /etc/nginx/sites-available/upload with the following (make sure to change example.com and www.example.com to your domain in the server_name directive and the access_log and error_log directives):
After creating the configuration files, unlink the default site and link the two new sites:
After saving the supplemental files and making the site configuration changes, check the Nginx configuration by running:
If there are no errors, Nginx returns:
At this point, restart Nginx by running service nginx restart
Now that the web server is ready, create Vultr Object Storage. Create Object Storage by logging in to console.vultr.com and navigating to Products -> Objects. Add Object Storage and give it a label. After creation, take note of the Hostname, the Secret Key, and the Access Key.
upload user by running su - upload.When changing to this user, you might receive a warning. This is OK.
Using the credentials from Object Storage, create /opt/php/config.php with the following contents - changing the values to suit your preferences:
After changing the values to accommodate your needs, save and close the file.
The following steps are still ran as the upload user
Use composer to get the core S3 API files. To download these files, change to the web site directory and run the following:
This installs various libraries used in the next step.
Ensure you're in the web site directory:
Create a directory structure to support the uploads:
To support the upload add a style sheet. Create /var/www/upload/css/main.css with the following:
Save the file and exit.
To complete the upload task, JavaScript provides the back end. Create /var/www/upload/js/main.js with the following:
Save the file and exit.
Create the supporting PHP functions file /var/www/upload/functions/main.php with the following content:
Save the file and exit.
Create /var/www/upload/php.upload.php which is the file that takes the upload data from JavaScript (configured above in the uploadFile(file) block of main.js). Add the following to this file:
Save the file and exit.
The last file that ties it all together is the HTML form that references the files above. Create /var/www/upload/index.php with the following content:
Save the file and exit.
Leaving the default settings creates a log file /var/log/upload/upload.log which shows every upload. The files are not stored on the local drive by default, they get uploaded to Object Storage and they can only be accessible by logging in. This is configured in the settings in /opt/php/config.php. This file also controls the Object Storage permissions. Setting BUCKET_ACL to public-read makes the files publicly available. The public location is https://S3_HOST_NAME/BUCKET_NAME/FILENAME.ext
After configuring the web server, install and secure it with LetsEncrypt.
Vultr Object Storage is compatible with a subset of the S3 API. See our compatibility matrix for details. Vultr Object Storage is a great storage tool and with PHP and a simple web server, makes a robust storage and delivery solution. Using technologies like these make files available and more accessible for people around the world.
0 Comments
Be the first to comment and share your perspective with the community.