Setting up a $5 Staging Server with Forge, Envoyer, and Nginx
1. Spin up a new server with Forge on Digital Ocean for $5 bucks.
If you learn better through video, check out the free Forge series on Laracasts - Server Management With Forge
2. Set up Envoyer to auto-deploy from your staging branch
Note: Be sure to check the PHP7 option when setting up your new server in Envoyer. If this is a new server created in Forge, it is PHP7 by default.
3. Set up your hosts file to point to your digital ocean IP
Check out how to setup a Local DNS record under the Local DNS section of this link. This is not a required step but adds a few conveniences for you.
- This will allow you to access your staging site using a URL like http://staging.mysite.com instead of using the IP address of your server or setting up a DNS record that you likely don't want exposed to the world.
- For picky OAuth providers (Twitter), you can provide a
127.0.0.1. Use the address you have set up in your hosts file and you should be good.
4. Configure NGINX for Basic Auth
In the case that someone does stumble upon your staging box, it would be nice to have at least a single layer of security between them and the secret goods you're working on. Use this guide to setup Basic Auth on a Digital Ocean NGINX box. It takes all of five minutes and will help you sleep at night. Use this key generator to generate a random and secure password for your Basic Auth setup.
Note: Basic Auth can be pretty dang susceptible to brute force attacks. Use a lenghty random password and don't expose anything on your staging site that could compromise you or your users.
How to log out of Basic Auth
If your site is http://staging.mysite.com, prefix your site address with
log:out@ so it would look like http://log:firstname.lastname@example.org. This sends through a request with the username of log and a password of out which will return a 401 and log you out of the site.
I've been using this for a few months and have been pretty happy with it. Hope this can help a few of you who are looking to do the same! Good luck.
Notes: A few commenters mentioned that using a $5 server equipped with 512MB of RAM can cause some problems when running
composer install or
npm install. You can read the following post to learn how to set up a swap file on digital ocean. Thanks to @jbpapp and @simondavies for the tip!