Author: David

  • Deploy your WordPress site easily on levv cloud

    Deploy your WordPress site easily on levv cloud

    WordPress is the world leading Content Management System (CMS), based on PHP and MySQL.

    In this tutorial, I will show you how to deploy your WordPress site on levv cloud, the lightweight and eco-responsible cloud platform. We will also deploy a persistent database for storing your content, using MySQL backed by a persistent volume.

    Let’s dive in!

    levv cloud storage

    Prerequisites to deploy your wordpress site

    Make sure you have the latest version of the levv CLI installed and available in your $PATH. You can download and install the levv CLI here.

    If you don’t have one already, you will need to create an account on levv cloud, and authenticate using the levv auth login command.

    You will also be prompted to create a default project, which we can use to deploy our WordPress application (ours is named "wordpress-project").

    Dockerizing WordPress

    Levv cloud services are based on containers, which are lightweight and portable units of software.

    At levv, we love Alpine Linux for our containers, as it is very light and easy to use. For this tutorial, we will use the official WordPress image running PHP 8.4 on Alpine Linux.

    Creating persistent storage

    By default, container data is lost after a crash or restart. To persist the WordPress database, we will create a persistent volume.

    Run the following command to create a volume of 2Gb inside your levv project.

    $ levv volumes create --size 2 wordpress-data

    Volumes can also be defined inside the compose file. If the specified volume does not exist yet, you will be prompted to create it.

    Deploying the application

    To deploy containers, our personal favorite is the compose format because of it’s simplicity and ease of use.

    Levv cloud uses a compose-compliant specification very similar to Docker Compose to define it’s components, so if you are familiar with Docker you should find your way around pretty quickly.

    Let’s create the following YAML file and save it as levv-wordpress.yaml.

    services:
      db:
        image: mysql:8.0
        environment:
          MYSQL_DATABASE: wordpress
          MYSQL_ROOT_PASSWORD: myR00tPassw0rd4567$$$
          MYSQL_USER: wordpress
          MYSQL_PASSWORD: wordpress
        labels:
          io.levv.size: "small"
        volumes:
          - wordpress-data:/var/lib/mysql
        ports:
          - 3306:3306  # Make db accessible to the wordpress service
      wordpress:
        image: wordpress:php8.4-fpm-alpine
        deploy:
          replicas: 1
        environment:
          WORDPRESS_DB_USER: wordpress
          WORDPRESS_DB_PASSWORD: wordpress
          WORDPRESS_DB_NAME: wordpress
        labels:
          io.levv.public: "true"    
        ports:
          - 80:8080
    
    volumes:
      wordpress-data:

    This is the simplest, albeit not secure, way to deploy WordPress, so do not use this example in production.

    Note: when using the ports key, there are two key differences compared to running your compose file on your local machine:

    1. By default, and for security reasons, services are not reachable by other services at all. If you want another service within the same service to access it, you need to define the ports key.
    2. Using the ports key does not make your service available to the outside world. You need to use a label for that, as explained in the section below.

    Next, we will deploy our compose file to levv cloud :

    $ levv apply -f levv-wordpress.yaml

    Once our services have been deployed, we will see the following output in the terminal:

    No project name has been specified, your default project will be used (wordpress-project).
    Getting project volumes    successful
    Applying specification    successful
    
    SERVICE    PROJECT  REPLICAS  SIZE  PUBLIC URL                                STATUS    
    db         test     1/1       small <none>                                    DEPLOYED  
    wordpress  test     1/1       nano  https://wordpress-test-c0a0.svc.levv.io   DEPLOYED  
    

    For more information about the levv compose specification, click here.

    Exposing your application on the internet

    By default, services deployed on levv cloud are not publicly accessible.

    To expose our WordPress site to the internet, we added the io.levv.public label to the wordpress service in the compose file. As you can notice, the WordPress service does have a public URL, while the database db service does not.

    Our WordPress site will be served using HTTPS, using SSL certificates from Let’s Encrypt.

    Note: levv takes care of SSL termination. So even if you expose port 80 on the container, the service will be accessible on port 443 via it’s public URL.

    We can also link our service to a custom domain name, using the domainname attribute in the compose file.

    For example:

    service:
      wordpress:    
        labels:
          io.levv.public: "true"
        domainname: blog.mydomain.be

    We then need to add a CNAME record to our Domain Name Servers pointing towards the public URL of our WordPress service.

    Conclusion

    In this tutorial, we deployed a WordPress site to levv cloud with a single compose file.

    Here’s a recap of what we did to deploy you wordpress site :

    • Create a levv compose file to define a WordPress service and a MySQL service
    • Create a persistent volume for the database service
    • Expose our WordPress service to the world using labels and custom domain names

    As you can see, deploying service on levv is very easy and secure by design.

    If you find this article helpful, or if you have any questions related to deploying your applications on levv cloud, feel free to drop me a line in the comments below!

    Want to know more about levv’s cloud? Check our article explaining why choose an eco-friendly cloud solution like levv?