{"id":14678,"date":"2022-10-28T17:38:24","date_gmt":"2022-10-28T15:38:24","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=14678"},"modified":"2022-10-28T17:38:30","modified_gmt":"2022-10-28T15:38:30","slug":"setting-up-wordpress-with-cloud-init","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/setting-up-wordpress-with-cloud-init\/","title":{"rendered":"Setting Up WordPress with Cloud-Init"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head.jpeg\" alt=\"\" class=\"wp-image-14680\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head.jpeg 1200w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head-600x315.jpeg 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head-768x403.jpeg 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p>WordPress is the most used open-source content management system &#8211; and it\u2019s totally free. More than 40% of the websites on the internet are powered by WordPress*, both small blogs and large websites. WordPress is extremely customizable and beginner-friendly, but is also suitable for large and complex websites.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-automate-the-setup-process-with-cloud-init\">Automate the Setup Process with Cloud-Init<\/h2>\n\n\n\n<p>Setting up new WordPress instances is easy, but in order for WordPress to run you need a webserver and a MySQL database. Setting all of this up can take a lot of time, especially if you&#8217;re doing it on a large scale.<\/p>\n\n\n\n<p>However, if you want to add WordPress to your existing server, head over to <a href=\"https:\/\/contabo.com\/blog\/how-to-install-wordpress\/\">this guide on setting up WordPress manually<\/a>.<\/p>\n\n\n\n<p>Cloud-Init can be used to automate the installation and setup process of your server by following a specific set of instructions you define. So in this guide, we will take a look at how the installation process looks and create a Cloud-Init config for it, so it can be automated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-benefits-of-using-cloud-init\">Benefits of using Cloud-Init<\/h2>\n\n\n\n<p>The true power of Cloud-Init is not only automating the initial installation, but replicating it on multiple machines &#8211; automatically. This is a huge time-saver in case of a disaster, where you need a new server running and configured as fast as possible. For more details about Cloud-Init itself, we\u2019ve written <a href=\"https:\/\/contabo.com\/blog\/what-is-cloud-init\/\">a whole article about the benefits and how to use it<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-wordpress-requirements\">WordPress Requirements<\/h2>\n\n\n\n<p>Before we can get into the installation process, we need to know what we actually need to install. WordPress recommends<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>servers running version 7.4 or greater of PHP,<\/li><li>MySQL version 5.7 OR MariaDB version 10.3 or greater,<\/li><li>either Apache or Nginx as the most robust options for running WordPress, but neither is required.<\/li><\/ul>\n\n\n\n<p>So basically WordPress is using the LAMP stack (with possible variations), which stands for Linux, Apache, MySQL \/ MariaDB and PHP. We\u2019ll install <strong>MariaDB version 10.3 and PHP 8.1<\/strong>, since this is the latest stable release of PHP (as of October 2022).<\/p>\n\n\n\n<p>You can use either a VPS, VDS, or a dedicated server, depending on the scale of your needs. Same for the operating system, you can use any Linux distribution. A common choice is a <a href=\"https:\/\/contabo.com\/en\/vps\/vps-m-ssd\/?image=debian.290&amp%3Bqty=1&amp%3Bcontract=1&amp%3Bstorage-type=vps-m-nvme-100-gb\">VPS M with NVMe storage and Debian 11<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-lamp-stack-setup\">LAMP Stack Setup<\/h2>\n\n\n\n<p>We\u2019ve already created a <a href=\"https:\/\/contabo.com\/blog\/lamp-stack-cloud-init\/\">guide on how to set up LAMP stack with Cloud-Init<\/a>, so we\u2019ll use this as a starting point for our new Cloud-Init config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#cloud-config\npackage_update: true\npackage_upgrade: true\npackages:\n  - software-properties-common\n  - wget\n  - curl\n  - apache2\nruncmd:\n  - curl -sSL https:\/\/packages.sury.org\/php\/README.txt | sudo bash -x\n  - sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-xsl php8.1-zip php8.1-bz2 libapache2-mod-php8.1 -y\n  - service apache2 restart\n  - apt install mariadb-server mariadb-client -y\n  - pw=$(openssl rand -base64 18); mysqladmin -u root -h localhost password \"$pw\"; echo \"mysql_password=$pw\" &gt;&gt; \/home\/mysql_access.txt\n  - mysqladmin reload\n<\/code><\/pre>\n\n\n\n<p>This installs Apache2 with PHP 8.1 and MariaDB. The password for our MariaDB database is stored in this file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/home\/mysql_access.txt<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-wordpress-installation\">WordPress Installation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-database-creation\">Database Creation<\/h3>\n\n\n\n<p>This step is pretty easy, all we need to do is execute one SQL command to create a new, empty database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE DATABASE wordpress;<\/code><\/pre>\n\n\n\n<p>So let\u2019s add this to our config:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Create database\n- sudo mysql -e \u201cCREATE DATABASE wordpress;\u201d\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-wordpress-cli\">WordPress CLI<\/h3>\n\n\n\n<p>WordPress is well-known for its ease of installation, and that also applies to headless installations (without opening the visual installer in the browser). The WordPress CLI provides a very easy way to install and configure a WordPress installation. So first, let\u2019s install the CLI:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  # Install WordPress CLI\n  - cd ~ \n  - sudo curl -O https:\/\/raw.githubusercontent.com\/wp-cli\/builds\/gh-pages\/phar\/wp-cli.phar\n  - sudo chmod +x wp-cli.phar\n  - sudo mv wp-cli.phar \/usr\/local\/bin\/wp\n<\/code><\/pre>\n\n\n\n<p>This will download the CLI file to the home directory, make it executable and move it to the bin directory so the \u201cwp\u201d command is globally available.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-wordpress-setup\">WordPress Setup<\/h3>\n\n\n\n<p>With the CLI installed, the actual setup is done in a few easy steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Navigate to the web server&#8217;s root directory (\/var\/www\/html)<\/li><li>Download the latest WordPress release<\/li><li>Configure the database connection<\/li><li>Install WordPress<\/li><\/ol>\n\n\n\n<p>Note: We will execute the commands as \u201cwww-data\u201d user since that\u2019s the user that\u2019s executing the PHP scripts. If we create the files and folders as root user, the www-data user won\u2019t have enough permissions to make changes to the directories (e.g. upload files, install plugins\/themes, \u2026).<\/p>\n\n\n\n<p>Prepare the html directory (set the correct permissions):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  # Set up WordPress\n  - cd \/var\/www\/\n  - sudo chown -R www-data:www-data html\n  - sudo chmod 755 html\n  - cd html\n<\/code><\/pre>\n\n\n\n<p>Also, delete the default html file that was created during the Apache2 installation. By default, Apache2 is configured to check for an index.html file first (before index.php), which means even with WordPress installed you will still see the default html file from Apache2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;- sudo rm index.html<\/code><\/pre>\n\n\n\n<p>Download the WordPress core:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- sudo -u www-data wp core download<\/code><\/pre>\n\n\n\n<p>Configure the MySQL database with the credentials and DB name as set before:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- sudo -u www-data wp core config --dbname='wordpress' --dbuser='root' --dbpass=\"$pw\" --dbhost='localhost' --dbprefix='wp_'<\/code><\/pre>\n\n\n\n<p>Make sure WordPress can access the \u201cwp-content\u201d directory (themes \/ plugins \/ file-uploads \/ \u2026):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;- sudo chmod -R 755 \/var\/www\/html\/wp-content<\/code><\/pre>\n\n\n\n<p>Now WordPress is downloaded and connected to the MySQL database. However, to complete the installation, we need to create the admin user and provide some details about the website.<\/p>\n\n\n\n<p>If you prefer to complete that step in a visual interface in the browser, you can skip the next command. Once the server is ready, open the server\u2019s IP in the browser and the WordPress installer shows up for this last step.<\/p>\n\n\n\n<p>To complete the installation automatically with cloud-init, use this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- sudo -u www-data wp core install --url='http:\/\/your-wordpress-website.com' --title='My WordPress Website' --admin_user='admin' --admin_password='secure' --admin_email='admin@example.com'<\/code><\/pre>\n\n\n\n<p>Make sure to replace the options with your real information.<\/p>\n\n\n\n<p>One final command: WordPress itself and many plugins are using .htaccess files to do things like<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>custom permalinks,<\/li><li>prevent access to specific files and folders,<\/li><li>enable caching,<\/li><\/ul>\n\n\n\n<p>and much more. To allow .htaccess files, create a new file in the Apache2 config folder that sets \u201cAllowOverride\u201d to All (for the directory \/var\/www\/html), enable the rewrite module, and restart Apache2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- sudo echo -e \"&lt;Directory \/var\/www\/html&gt;\\n    AllowOverride All\\n&lt;\/Directory&gt;\" &gt;&gt; \/etc\/apache2\/sites-enabled\/allow-htaccess.conf\n  - sudo a2enmod rewrite\n  - sudo service apache2 restart\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-final-cloud-init-config\">Final Cloud-Init Config<\/h2>\n\n\n\n<p>All the steps combined, that\u2019s how the final Cloud-Init configuration file looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#cloud-config\npackage_update: true\npackage_upgrade: true\npackages:\n  - software-properties-common\n  - wget\n  - curl\n  - apache2\nruncmd:\n  - curl -sSL https:\/\/packages.sury.org\/php\/README.txt | sudo bash -x\n  - sudo apt-get install php8.1 php8.1-cli php8.1-common php8.1-curl php8.1-gd php8.1-intl php8.1-mbstring php8.1-mysql php8.1-opcache php8.1-readline php8.1-xml php8.1-xsl php8.1-zip php8.1-bz2 libapache2-mod-php8.1 -y\n  - service apache2 restart\n  - apt install mariadb-server mariadb-client -y\n  - pw=$(openssl rand -base64 18); mysqladmin -u root -h localhost password \"$pw\"; echo \"mysql_password=$pw\" &gt;&gt; \/home\/mysql_access.txt\n  - mysqladmin reload\n\n  # Create database\n  - sudo mysql -e \"CREATE DATABASE wordpress;\"\n\n  # Install WordPress CLI\n  - cd ~ \n  - sudo curl -O https:\/\/raw.githubusercontent.com\/wp-cli\/builds\/gh-pages\/phar\/wp-cli.phar\n  - sudo chmod +x wp-cli.phar\n  - sudo mv wp-cli.phar \/usr\/local\/bin\/wp\n\n  # Set up WordPress\n  - cd \/var\/www\/\n  - sudo chown -R www-data:www-data html\n  - sudo chmod 755 html\n  - cd html\n  - sudo rm index.html\n  - sudo -u www-data wp core download\n  - sudo -u www-data wp core config --dbname='wordpress' --dbuser='root' --dbpass=\u201d$pw\u201d --dbhost='localhost' --dbprefix='wp_'\n  - sudo chmod -R 755 \/var\/www\/html\/wp-content\n\n  # Install WordPress\n  - sudo -u www-data wp core install --url='http:\/\/your-wordpress-website.com' --title='My WordPress Website' --admin_user='admin' --admin_password='secure' --admin_email='admin@example.com'\n\n  # Allow .htaccess files\n  - sudo echo -e \"&lt;Directory \/var\/www\/html&gt;\\n    AllowOverride All\\n&lt;\/Directory&gt;\" &gt;&gt; \/etc\/apache2\/sites-enabled\/allow-htaccess.conf\n  - sudo a2enmod rewrite\n  - sudo service apache2 restart\n<\/code><\/pre>\n\n\n\n<p>(*) According to the <a href=\"https:\/\/wordpress.org\/\" rel=\"nofollow\">official WordPress website<\/a> and <a href=\"https:\/\/w3techs.com\/technologies\/details\/cm-wordpress\" rel=\"nofollow\">W3Techs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up a WordPress instance can be done in seconds with the help of Cloud-init. In this article we show you how it&#8217;s done.<\/p>\n","protected":false},"author":57,"featured_media":14680,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[1525],"class_list":["post-14678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head.jpeg",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head-150x150.jpeg",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head-600x315.jpeg",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head-768x403.jpeg",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head.jpeg",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head.jpeg",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2022\/10\/blog-head.jpeg",1200,630,false]},"uagb_author_info":{"display_name":"Linus Benkner","author_link":"https:\/\/contabo.com\/blog\/author\/linus\/"},"uagb_comment_info":0,"uagb_excerpt":"Setting up a WordPress instance can be done in seconds with the help of Cloud-init. In this article we show you how it's done.","authors":[{"term_id":1525,"user_id":57,"is_guest":0,"slug":"linus","display_name":"Linus Benkner","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/396885b0c71ca364e267b5804deaef19e48538c136b5287377b0d481091abd10?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/14678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=14678"}],"version-history":[{"count":0,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/14678\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/14680"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=14678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=14678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=14678"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=14678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}