{"id":338,"date":"2025-04-14T12:39:02","date_gmt":"2025-04-14T12:39:02","guid":{"rendered":"https:\/\/blog.hostinger.io\/support\/2025\/04\/14\/11080657-using-post-install-scripts-with-hostinger-api\/"},"modified":"2025-04-14T12:39:02","modified_gmt":"2025-04-14T12:39:02","slug":"11080657-using-post-install-scripts-with-hostinger-api","status":"publish","type":"post","link":"https:\/\/www.hostinger.com\/support\/11080657-using-post-install-scripts-with-hostinger-api\/","title":{"rendered":"Using Post-Install Scripts With Hostinger API"},"content":{"rendered":"<p class=\"no-margin\">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.<\/p><p class=\"no-margin\">\n<\/p><h2 id=\"h_c6990fff37\">What are post-install scripts?<\/h2><p class=\"no-margin\">Post-install scripts are custom shell scripts that run automatically after your VPS is deployed. They allow you to:<\/p><ul>\n<li>\n<p class=\"no-margin\">Automate the initial setup of your server<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Install software packages<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Configure services<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Set up security measures<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Deploy applications<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">And much more<\/p>\n<\/li>\n<\/ul><p class=\"no-margin\">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.<\/p><p class=\"no-margin\">\n<\/p><h2 id=\"h_7969a66d09\">Technical details<\/h2><ul>\n<li>\n<p class=\"no-margin\">Scripts are saved to the file <code>\/post_install<\/code> on your VPS with executable permissions<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Output from script execution is saved to <code>\/post_install.log<\/code><\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Maximum script size is 48KB<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Scripts are executed with root privileges<\/p>\n<\/li>\n<\/ul><h2 id=\"h_76028551d6\">Managing post-install scripts with the API<\/h2><p class=\"no-margin\">The Hostinger API provides several endpoints to manage your post-install scripts:<\/p><p class=\"no-margin\">\n<\/p><h3 id=\"h_dc25e20e1f\">1. Creating a post-install script<\/h3><p class=\"no-margin\">To create a new post-install script:<\/p><pre><code>POST \/api\/vps\/v1\/post-install-scripts<\/code><\/pre><p class=\"no-margin\">Required parameters:<\/p><ul>\n<li>\n<p class=\"no-margin\"><code>name<\/code>: A descriptive name for your script<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><code>content<\/code>: The actual shell script content<\/p>\n<\/li>\n<\/ul><p class=\"no-margin\">Example request:<\/p><pre><code>{<br>  \"name\": \"LAMP Stack Setup\",<br>  \"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!' &gt; \/var\/www\/html\/index.html\"<br>}<\/code><\/pre><h3 id=\"h_86317ddc2a\">2. Getting a list of post-install scripts<\/h3><p class=\"no-margin\">To retrieve all your post-install scripts:<\/p><pre><code>GET \/api\/vps\/v1\/post-install-scripts<\/code><\/pre><p class=\"no-margin\">Optional parameters:<\/p><ul>\n<li>\n<p class=\"no-margin\"><code>page<\/code>: Page number for pagination<\/p>\n<\/li>\n<\/ul><h3 id=\"h_5e0b81f341\">3. Getting a specific post-install script<\/h3><p class=\"no-margin\">To retrieve details of a specific post-install script:<\/p><pre><code>GET \/api\/vps\/v1\/post-install-scripts\/{postInstallScriptId}<\/code><\/pre><p class=\"no-margin\">Required parameters:<\/p><ul>\n<li>\n<p class=\"no-margin\"><code>postInstallScriptId<\/code>: The ID of the post-install script<\/p>\n<\/li>\n<\/ul><h3 id=\"h_2f56d238c3\">4. Updating a post-install script<\/h3><p class=\"no-margin\">To update an existing post-install script:<\/p><pre><code>PUT \/api\/vps\/v1\/post-install-scripts\/{postInstallScriptId}<\/code><\/pre><p class=\"no-margin\">Required parameters:<\/p><ul>\n<li>\n<p class=\"no-margin\"><code>postInstallScriptId<\/code>: The ID of the post-install script<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><code>name<\/code>: Updated name for the script<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><code>content<\/code>: Updated script content<\/p>\n<\/li>\n<\/ul><h3 id=\"h_71aefdddf7\">5. Deleting a post-install script<\/h3><p class=\"no-margin\">To delete a post-install script:<\/p><pre><code>DELETE \/api\/vps\/v1\/post-install-scripts\/{postInstallScriptId}<\/code><\/pre><p class=\"no-margin\">Required parameters:<\/p><ul>\n<li>\n<p class=\"no-margin\"><code>postInstallScriptId<\/code>: The ID of the post-install script to delete<\/p>\n<\/li>\n<\/ul><h2 id=\"h_b823fdeb6b\">Using post-install scripts with VPS instances<\/h2><p class=\"no-margin\">When creating or recreating a VPS, you can specify a post-install script to run by passing the <code>post_install_script_id<\/code> parameter:<\/p><p class=\"no-margin\">\n<\/p><h3 id=\"h_21e89b794c\">For new VPS setup<\/h3><pre><code>POST \/api\/vps\/v1\/virtual-machines\/{virtualMachineId}\/setup<\/code><\/pre><p class=\"no-margin\">Include the <code>post_install_script_id<\/code> parameter along with other required parameters:<\/p><pre><code>{ \"data_center_id\": 1, \"template_id\": 4, \"post_install_script_id\": 12 }<\/code><\/pre><h3 id=\"h_57a143641d\">For VPS recreation<\/h3><pre><code>POST \/api\/vps\/v1\/virtual-machines\/{virtualMachineId}\/recreate<\/code><\/pre><p class=\"no-margin\">Include the <code>post_install_script_id<\/code> parameter:<\/p><pre><code>{ \"template_id\": 4, \"post_install_script_id\": 12 }<\/code><\/pre><h2 id=\"h_ea52ef989b\">Practical examples<\/h2><p class=\"no-margin\">Here are some useful examples of post-install scripts for common scenarios:<\/p><h3 id=\"h_39ef1973f5\">Basic security setup<\/h3><pre><code>#!\/bin\/bash<br><br># Update system packages<br>apt-get update<br>apt-get upgrade -y<br><br># Install fail2ban for protection against brute force attacks<br>apt-get install -y fail2ban<br>systemctl enable fail2ban<br>systemctl start fail2ban<br><br># Configure firewall<br>apt-get install -y ufw<br>ufw default deny incoming<br>ufw default allow outgoing<br>ufw allow ssh<br>ufw allow http<br>ufw allow https<br>ufw --force enable<br><br># Create a non-root user with sudo privileges<br>useradd -m -s \/bin\/bash admin<br>echo \"admin ALL=(ALL) NOPASSWD:ALL\" &gt; \/etc\/sudoers.d\/admin<br>mkdir -p \/home\/admin\/.ssh<br>chmod 700 \/home\/admin\/.ssh<br><br># Harden SSH configuration<br>sed -i 's\/PermitRootLogin yes\/PermitRootLogin no\/' \/etc\/ssh\/sshd_config<br>sed -i 's\/#PasswordAuthentication yes\/PasswordAuthentication no\/' \/etc\/ssh\/sshd_config<br>systemctl restart sshd<br><br>echo \"Basic security setup completed!\"<\/code><\/pre><h3 id=\"h_f41e321bcf\">WordPress installation<\/h3><pre><code>#!\/bin\/bash<br><br># Update system packages<br>apt-get update<br>apt-get upgrade -y<br><br># Install LAMP stack<br>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<br><br># Enable Apache modules<br>a2enmod rewrite<br>systemctl restart apache2<br><br># Set up MySQL (non-interactive)<br>mysql_root_password=\"$(openssl rand -base64 12)\"<br>echo \"MySQL root password: $mysql_root_password\" &gt; \/root\/mysql_credentials.txt<br>chmod 600 \/root\/mysql_credentials.txt<br><br># Secure MySQL installation<br>mysql -e \"UPDATE mysql.user SET Password=PASSWORD('$mysql_root_password') WHERE User='root';\"<br>mysql -e \"DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');\"<br>mysql -e \"DELETE FROM mysql.user WHERE User='';\"<br>mysql -e \"DROP DATABASE IF EXISTS test;\"<br>mysql -e \"DELETE FROM mysql.db WHERE Db='test' OR Db='test\\\\_%';\"<br>mysql -e \"FLUSH PRIVILEGES;\"<br><br># Create WordPress database<br>db_name=\"wordpress\"<br>db_user=\"wpuser\"<br>db_password=\"$(openssl rand -base64 12)\"<br><br>mysql -u root -p\"$mysql_root_password\" -e \"CREATE DATABASE $db_name;\"<br>mysql -u root -p\"$mysql_root_password\" -e \"CREATE USER '$db_user'@'localhost' IDENTIFIED BY '$db_password';\"<br>mysql -u root -p\"$mysql_root_password\" -e \"GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'@'localhost';\"<br>mysql -u root -p\"$mysql_root_password\" -e \"FLUSH PRIVILEGES;\"<br><br>echo \"WordPress database credentials:\" &gt; \/root\/wordpress_credentials.txt<br>echo \"Database: $db_name\" &gt;&gt; \/root\/wordpress_credentials.txt<br>echo \"Username: $db_user\" &gt;&gt; \/root\/wordpress_credentials.txt<br>echo \"Password: $db_password\" &gt;&gt; \/root\/wordpress_credentials.txt<br>chmod 600 \/root\/wordpress_credentials.txt<br><br># Download and set up WordPress<br>cd \/var\/www\/html<br>rm index.html<br>wget https:\/\/wordpress.org\/latest.tar.gz<br>tar -xzf latest.tar.gz<br>mv wordpress\/* .<br>rmdir wordpress<br>rm latest.tar.gz<br><br># Configure WordPress<br>cp wp-config-sample.php wp-config.php<br>sed -i \"s\/database_name_here\/$db_name\/\" wp-config.php<br>sed -i \"s\/username_here\/$db_user\/\" wp-config.php<br>sed -i \"s\/password_here\/$db_password\/\" wp-config.php<br><br># Set up salts<br>curl https:\/\/api.wordpress.org\/secret-key\/1.1\/salt\/ &gt; salts.txt<br>sed -i '\/AUTH_KEY\/d' wp-config.php<br>sed -i '\/SECURE_AUTH_KEY\/d' wp-config.php<br>sed -i '\/LOGGED_IN_KEY\/d' wp-config.php<br>sed -i '\/NONCE_KEY\/d' wp-config.php<br>sed -i '\/AUTH_SALT\/d' wp-config.php<br>sed -i '\/SECURE_AUTH_SALT\/d' wp-config.php<br>sed -i '\/LOGGED_IN_SALT\/d' wp-config.php<br>sed -i '\/NONCE_SALT\/d' wp-config.php<br>sed -i \"\/\\$table_prefix\/r salts.txt\" wp-config.php<br>rm salts.txt<br><br># Set permissions<br>chown -R www-data:www-data \/var\/www\/html\/<br>find \/var\/www\/html\/ -type d -exec chmod 755 {} \\;<br>find \/var\/www\/html\/ -type f -exec chmod 644 {} \\;<br><br>echo \"WordPress installation completed!\"<\/code><\/pre><h3 id=\"h_0626dde7ac\">Docker environment setup<\/h3><pre><code>#!\/bin\/bash<br><br># Update system packages<br>apt-get update<br>apt-get upgrade -y<br><br># Install prerequisites<br>apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg<br><br># Add Docker's official GPG key<br>curl -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg | apt-key add -<br><br># Set up the Docker repository<br>add-apt-repository \"deb [arch=amd64] https:\/\/download.docker.com\/linux\/ubuntu $(lsb_release -cs) stable\"<br><br># Install Docker<br>apt-get update<br>apt-get install -y docker-ce docker-ce-cli containerd.io<br><br># Install Docker Compose<br>curl -L \"https:\/\/github.com\/docker\/compose\/releases\/download\/v2.17.0\/docker-compose-$(uname -s)-$(uname -m)\" -o \/usr\/local\/bin\/docker-compose<br>chmod +x \/usr\/local\/bin\/docker-compose<br><br># Enable Docker to start on boot<br>systemctl enable docker<br>systemctl start docker<br><br># Create a non-root user and add to the docker group<br>useradd -m -s \/bin\/bash dockeruser<br>usermod -aG docker dockeruser<br><br># Create a simple test container<br>docker run -d -p 80:80 --name test-nginx nginx<br><br>echo \"Docker environment setup completed!\"<\/code><\/pre><h2 id=\"h_6d87e4e197\">Best practices<\/h2><ol>\n<li>\n<p class=\"no-margin\"><b>Test thoroughly<\/b>: Always test your scripts in a controlled environment before using them in production.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><b>Error handling<\/b>: Include proper error handling in your scripts to catch and report issues during execution.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><b>Logging<\/b>: Add informative log messages to track the progress and identify problems.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><b>Security<\/b>: Be careful with sensitive information. Don&rsquo;t hardcode credentials or sensitive data in your scripts.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><b>Idempotence<\/b>: Design your scripts to be idempotent, meaning they can be run multiple times without causing issues.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><b>Timeouts<\/b>: Consider that some operations, especially package installations, might take longer than expected.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\"><b>Version pinning<\/b>: Specify exact versions of packages to ensure consistent deployments.<\/p>\n<\/li>\n<\/ol><h2 id=\"h_6af87d15b8\">Troubleshooting<\/h2><ul>\n<li>\n<p class=\"no-margin\">If your script fails, check the <code>\/post_install.log<\/code> file on your VPS for error messages.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Ensure your script has a proper shebang line (e.g., <code>#!\/bin\/bash<\/code>) at the beginning.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Remember that the script runs with root privileges, so you don&rsquo;t need to use <code>sudo<\/code> within the script.<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">If your script appears to hang, check if any commands await user input (e.g., apt-get might ask for confirmation).<\/p>\n<\/li>\n<li>\n<p class=\"no-margin\">Make sure to remove any hidden and invisible characters. Try to re-save it with Unix \/ LF line endings before uploading.<\/p>\n<\/li>\n<\/ul><h2 id=\"h_d4e325bb8d\"><\/h2><p class=\"no-margin\">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.<\/p><p class=\"no-margin\">For more information about the Hostinger API, visit <a href=\"https:\/\/api.hostinger.com\/docs\/\" target=\"_blank\" class=\"intercom-content-link\" rel=\"noopener\">Hostinger API Documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Article about using post-install scripts with the Hostinger API<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"include_on_kodee":true,"footnotes":""},"categories":[295],"tags":[],"class_list":["post-338","post","type-post","status-publish","format-standard","hentry","category-api"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/support\/11080657-using-post-install-scripts-with-hostinger-api\/","default":1}],"include_on_kodee":true,"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/comments?post=338"}],"version-history":[{"count":0,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/338\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/media?parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/categories?post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/tags?post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}