|
| 1 | +--- |
| 2 | +sidebar: false |
| 3 | +editLink: false |
| 4 | +prev: false |
| 5 | +next: false |
| 6 | +lastUpdated: false |
| 7 | +--- |
| 8 | + |
| 9 | +# Deploying a LeafMVC Application to Digital Ocean |
| 10 | + |
| 11 | +::: warning Version support |
| 12 | +Version support. This tutorial assumes use of LeafPHP >= 3.0 and PHP >=7.0. |
| 13 | +::: |
| 14 | + |
| 15 | +## What Are We Building |
| 16 | + |
| 17 | +This experiment will guide you deploying your first LeafMVC application to Digital Ocean. A majority |
| 18 | +of the same steps apply to Leaf v3 core as well. |
| 19 | + |
| 20 | +::: details (New to Digital Ocean?) |
| 21 | +Digital Ocean is a cloud service provider that offers great introductory pricing for virtual private |
| 22 | +servers (VPS). Create an account, tether a credit card, and prepare to build. |
| 23 | +::: |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +Before continuing, it is important to determine if you would like to purhcase or point a domain name |
| 28 | +to the VPS you are about to spin up. $DOMAIN will be shown several times throughout this experiment |
| 29 | +and should be replaced by either your domain name (example.com) or the Droplet's public IP address. You |
| 30 | +can grab the public IP address from the Digital Ocean control panel. |
| 31 | + |
| 32 | +For instructions on how to setup a domain with Digital Ocean, [click here](https://docs.digitalocean.com/products/networking/dns/how-to/add-domains/). |
| 33 | + |
| 34 | +## 1. Create a new droplet |
| 35 | + |
| 36 | +From the control panel, click the green "Create" button and select droplet. We will create a VPS with the |
| 37 | +following options selected: |
| 38 | + |
| 39 | +* Ubuntu: 20.04 (LTS) |
| 40 | +* Plan: Basic |
| 41 | +* CPU Options: Premium AMD or Regular Intel |
| 42 | +* $6/mo package |
| 43 | + |
| 44 | +::: tip Scaling ⚡️ |
| 45 | +Should your application grow in requirements or traffic, you can always come back and increase your package selection. |
| 46 | +::: |
| 47 | + |
| 48 | +### Authentication |
| 49 | + |
| 50 | +It is highly recommended that your utilize SSH-based authentication. Select an existing key, or [generate a new key](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent), then add it. |
| 51 | + |
| 52 | +## 2. Initial droplet setup |
| 53 | + |
| 54 | +After your droplet has been created, you will need to login, secure it, and install required software. The first task will |
| 55 | +be to create an admin user, then utilie that account for future SSH connections. |
| 56 | + |
| 57 | +```bash |
| 58 | +ssh root@$DOMAIN |
| 59 | +adduser username |
| 60 | +usermod -aG sudo username |
| 61 | +rsync --archive --chown=username:username ~/.ssh /home/username |
| 62 | +``` |
| 63 | + |
| 64 | +Test the admin account: ``su - username``. If the command executes, you can terminal the SSH session and log |
| 65 | +back in with your new user account (recommended). |
| 66 | + |
| 67 | +### Setup firewall |
| 68 | + |
| 69 | +Next we will setup UFW - Ubuntu Firewall. We will allow communication on ports: 22 (SSH), 80 (HTTP), and 443 (SSL). |
| 70 | + |
| 71 | +```bash |
| 72 | +sudo ufw allow 22 |
| 73 | +sudo ufw allow 80 |
| 74 | +sudo ufw allow 443 |
| 75 | +sudo ufw enable |
| 76 | +``` |
| 77 | + |
| 78 | +After creating the firewall's rules and enabling UFW, you can view firewall status by ``sudo ufw status``. |
| 79 | + |
| 80 | +### Install required software |
| 81 | + |
| 82 | +It is now time install all of the needed software to enable LeafPHP to run. First, we need to update all system software: |
| 83 | + |
| 84 | +```bash |
| 85 | +sudo apt update |
| 86 | +sudo apt upgrade |
| 87 | +``` |
| 88 | + |
| 89 | +Be sure to respond **Y** when asked to continue. Now we can intall NGINX, PHP, MySQL, and curl. |
| 90 | + |
| 91 | +```bash |
| 92 | +sudo apt install nginx php-fpm php-mysql php-curl |
| 93 | +``` |
| 94 | + |
| 95 | +Once complete, follow the |
| 96 | +[NGINX instructions](https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04#step-3-%E2%80%93-installing-php-and-configuring-nginx-to-use-the-php-processor). |
| 97 | +Ensure that your directory is set as such: `` root /var/www/$DOMAIN/public;`` Below is an example sites-available file. |
| 98 | + |
| 99 | +```nginx |
| 100 | +server { |
| 101 | + server_name itsglint.com www.itsglint.com 147.182.136.153; |
| 102 | + root /var/www/itsglint.com/public; |
| 103 | +
|
| 104 | + index index.html index.htm index.php; |
| 105 | +
|
| 106 | + location / { |
| 107 | + try_files $uri /index.php?$query_string; |
| 108 | + } |
| 109 | +
|
| 110 | + location ~ \.php$ { |
| 111 | + include snippets/fastcgi-php.conf; |
| 112 | + fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +::: warning Leaf Router and .htaccess support |
| 118 | +It is important to mirror the location blocks as-in. Otherwise, LeafRouter will not work properly or at all. |
| 119 | +::: |
| 120 | + |
| 121 | +Next, we will install Mysql. Follow the [install instructions](https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04#step-2-%E2%80%93-installing-mysql-to-manage-site-data). |
| 122 | + |
| 123 | +```bash |
| 124 | +sudo apt install mysql-server |
| 125 | +``` |
| 126 | + |
| 127 | +### Install your Leaf🍁 application |
| 128 | + |
| 129 | +Next, we will download and install our application with all dependencies. Clone your repository from Github, |
| 130 | +or any source, and place your Leaf project in ``/var/www/$DOMAIN``. Afterwards, install required dependencies |
| 131 | +and perform initial Leaf tasks: |
| 132 | + |
| 133 | +```bash |
| 134 | +composer install |
| 135 | +php leaf db:install |
| 136 | +php leaf db:migrate |
| 137 | +``` |
| 138 | + |
| 139 | +You also may seed the database if required: `php leaf db:seed`. |
| 140 | + |
| 141 | +Congratulations 🎉, you now have a fully working production server, and should be able to reach your application at $DOMAIN. |
| 142 | + |
| 143 | +::: details Recommended: Complete SSL Setup |
| 144 | +If your Leaf applications is more than a hobbyist adventure and serving actual clients or visitors, it is |
| 145 | +strongly recommended to complete the SSL setup. SSL encrypts traffic between a browser and server. Replace |
| 146 | +example.com with $DOMAIN. |
| 147 | + |
| 148 | +``` |
| 149 | +sudo apt install certbot python3-certbot-nginx |
| 150 | +sudo systemctl reload nginx |
| 151 | +sudo certbot --nginx -d example.com -d www.example.com |
| 152 | +``` |
| 153 | + |
| 154 | +When prompted for HTTPS redirction, select **Option 2**, forcing HTTPS traffic. |
| 155 | + |
| 156 | +::: |
| 157 | + |
| 158 | +<br> |
| 159 | + |
| 160 | +Experiment by **[Matthew Reichardt](https://github.com/matthewjamesr)** |
0 commit comments