Post-install scripts are powerful automation tools that can be executed automatically after installing your virtual machine (VPS). This article explains what post-install scripts are, how to create and manage them using the Hostinger API, and provides practical examples for common use cases.
What are post-install scripts?
Post-install scripts are custom shell scripts that run automatically after your VPS is deployed. They allow you to:
Automate the initial setup of your server
Install software packages
Configure services
Set up security measures
Deploy applications
And much more
When a post-install script is attached to a VPS instance, it is executed with root privileges immediately after the operating system installation is complete.
Technical details
Scripts are saved to the file
/post_install
on your VPS with executable permissionsOutput from script execution is saved to
/post_install.log
Maximum script size is 48KB
Scripts are executed with root privileges
Managing post-install scripts with the API
The Hostinger API provides several endpoints to manage your post-install scripts:
1. Creating a post-install script
To create a new post-install script:
POST /api/vps/v1/post-install-scripts
Required parameters:
name
: A descriptive name for your scriptcontent
: The actual shell script content
Example request:
{
"name": "LAMP Stack Setup",
"content": "#!/bin/bash\n\n# Update system packages\napt-get update\napt-get upgrade -y\n\n# Install LAMP stack\napt-get install -y apache2 mysql-server php libapache2-mod-php php-mysql\n\n# Enable services\nsystemctl enable apache2\nsystemctl enable mysql\n\n# Set up firewall\nufw allow 'Apache Full'\nufw allow 'OpenSSH'\n\necho 'LAMP stack installation complete!' > /var/www/html/index.html"
}
2. Getting a list of post-install scripts
To retrieve all your post-install scripts:
GET /api/vps/v1/post-install-scripts
Optional parameters:
page
: Page number for pagination
3. Getting a specific post-install script
To retrieve details of a specific post-install script:
GET /api/vps/v1/post-install-scripts/{postInstallScriptId}
Required parameters:
postInstallScriptId
: The ID of the post-install script
4. Updating a post-install script
To update an existing post-install script:
PUT /api/vps/v1/post-install-scripts/{postInstallScriptId}
Required parameters:
postInstallScriptId
: The ID of the post-install scriptname
: Updated name for the scriptcontent
: Updated script content
5. Deleting a post-install script
To delete a post-install script:
DELETE /api/vps/v1/post-install-scripts/{postInstallScriptId}
Required parameters:
postInstallScriptId
: The ID of the post-install script to delete
Using post-install scripts with VPS instances
When creating or recreating a VPS, you can specify a post-install script to run by passing the post_install_script_id
parameter:
For new VPS setup
POST /api/vps/v1/virtual-machines/{virtualMachineId}/setup
Include the post_install_script_id
parameter along with other required parameters:
{ "data_center_id": 1, "template_id": 4, "post_install_script_id": 12 }
For VPS recreation
POST /api/vps/v1/virtual-machines/{virtualMachineId}/recreate
Include the post_install_script_id
parameter:
{ "template_id": 4, "post_install_script_id": 12 }
Practical examples
Here are some useful examples of post-install scripts for common scenarios:
Basic security setup
#!/bin/bash
# Update system packages
apt-get update
apt-get upgrade -y
# Install fail2ban for protection against brute force attacks
apt-get install -y fail2ban
systemctl enable fail2ban
systemctl start fail2ban
# Configure firewall
apt-get install -y ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow http
ufw allow https
ufw --force enable
# Create a non-root user with sudo privileges
useradd -m -s /bin/bash admin
echo "admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/admin
mkdir -p /home/admin/.ssh
chmod 700 /home/admin/.ssh
# Harden SSH configuration
sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
systemctl restart sshd
echo "Basic security setup completed!"
WordPress installation
#!/bin/bash
# Update system packages
apt-get update
apt-get upgrade -y
# Install LAMP stack
apt-get install -y apache2 mysql-server php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip
# Enable Apache modules
a2enmod rewrite
systemctl restart apache2
# Set up MySQL (non-interactive)
mysql_root_password="$(openssl rand -base64 12)"
echo "MySQL root password: $mysql_root_password" > /root/mysql_credentials.txt
chmod 600 /root/mysql_credentials.txt
# Secure MySQL installation
mysql -e "UPDATE mysql.user SET Password=PASSWORD('$mysql_root_password') WHERE User='root';"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -e "DELETE FROM mysql.user WHERE User='';"
mysql -e "DROP DATABASE IF EXISTS test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -e "FLUSH PRIVILEGES;"
# Create WordPress database
db_name="wordpress"
db_user="wpuser"
db_password="$(openssl rand -base64 12)"
mysql -u root -p"$mysql_root_password" -e "CREATE DATABASE $db_name;"
mysql -u root -p"$mysql_root_password" -e "CREATE USER '$db_user'@'localhost' IDENTIFIED BY '$db_password';"
mysql -u root -p"$mysql_root_password" -e "GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'@'localhost';"
mysql -u root -p"$mysql_root_password" -e "FLUSH PRIVILEGES;"
echo "WordPress database credentials:" > /root/wordpress_credentials.txt
echo "Database: $db_name" >> /root/wordpress_credentials.txt
echo "Username: $db_user" >> /root/wordpress_credentials.txt
echo "Password: $db_password" >> /root/wordpress_credentials.txt
chmod 600 /root/wordpress_credentials.txt
# Download and set up WordPress
cd /var/www/html
rm index.html
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
mv wordpress/* .
rmdir wordpress
rm latest.tar.gz
# Configure WordPress
cp wp-config-sample.php wp-config.php
sed -i "s/database_name_here/$db_name/" wp-config.php
sed -i "s/username_here/$db_user/" wp-config.php
sed -i "s/password_here/$db_password/" wp-config.php
# Set up salts
curl https://api.wordpress.org/secret-key/1.1/salt/ > salts.txt
sed -i '/AUTH_KEY/d' wp-config.php
sed -i '/SECURE_AUTH_KEY/d' wp-config.php
sed -i '/LOGGED_IN_KEY/d' wp-config.php
sed -i '/NONCE_KEY/d' wp-config.php
sed -i '/AUTH_SALT/d' wp-config.php
sed -i '/SECURE_AUTH_SALT/d' wp-config.php
sed -i '/LOGGED_IN_SALT/d' wp-config.php
sed -i '/NONCE_SALT/d' wp-config.php
sed -i "/\$table_prefix/r salts.txt" wp-config.php
rm salts.txt
# Set permissions
chown -R www-data:www-data /var/www/html/
find /var/www/html/ -type d -exec chmod 755 {} \;
find /var/www/html/ -type f -exec chmod 644 {} \;
echo "WordPress installation completed!"
Docker environment setup
#!/bin/bash
# Update system packages
apt-get update
apt-get upgrade -y
# Install prerequisites
apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
# Set up the Docker repository
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Install Docker
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io
# Install Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.17.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# Enable Docker to start on boot
systemctl enable docker
systemctl start docker
# Create a non-root user and add to the docker group
useradd -m -s /bin/bash dockeruser
usermod -aG docker dockeruser
# Create a simple test container
docker run -d -p 80:80 --name test-nginx nginx
echo "Docker environment setup completed!"
Best practices
Test thoroughly: Always test your scripts in a controlled environment before using them in production.
Error handling: Include proper error handling in your scripts to catch and report issues during execution.
Logging: Add informative log messages to track the progress and identify problems.
Security: Be careful with sensitive information. Don't hardcode credentials or sensitive data in your scripts.
Idempotence: Design your scripts to be idempotent, meaning they can be run multiple times without causing issues.
Timeouts: Consider that some operations, especially package installations, might take longer than expected.
Version pinning: Specify exact versions of packages to ensure consistent deployments.
Troubleshooting
If your script fails, check the
/post_install.log
file on your VPS for error messages.Ensure your script has a proper shebang line (e.g.,
#!/bin/bash
) at the beginning.Remember that the script runs with root privileges, so you don't need to use
sudo
within the script.If your script appears to hang, check if any commands await user input (e.g., apt-get might ask for confirmation).
Post-install scripts are powerful tools that can significantly streamline your VPS deployment and configuration process. By leveraging the Hostinger API to manage and apply these scripts, you can ensure consistent, automated server setups across your infrastructure.
For more information about the Hostinger API, visit Hostinger API Documentation.