First off, a back story. You should jump ahead if you just want instructions on how to handle hosting Vapor on a Mac.
One of the things I've learned about myself over the years is that I need variety in my work. Every two or three years I look around and see what I could be doing differently to increase productivity and keep software development interesting.
I've had the opportunity over the past 15 years to do a lot of web development work for a variety of clients. Several years ago, after a particularly demanding project, it was clear to me that I needed to find something better on the server side (I was mostly doing PHP at the time). That's when I found Laravel. Laravel felt comfortable from the start, and I've been using it for the past 5 years. I've used it to power everything from simple sites with a few forms to larger sites that process several hundred orders per day. In short, if you program in PHP, you should check it out.
Last year, when I started working on our new website - I again had this feeling that I needed something different. Maybe it was the fact that I'd been using Swift a lot, and going back to PHP just felt... wrong. So, I set about searching to see if there was something that would allow me to stay in Swift-land, something again to make web development fun. I found Vapor.
This brings me to the other really cool thing about Swift: you can easily leverage existing macOS frameworks to build your backend. For me, this means I can utilize libraries like Core Image or Foundation, opening up a huge set of frameworks that I'm already familiar with. This also exposes what I consider a hole in the ecosystem - hosting. In order to take advantage of Apple frameworks, you need to be running on macOS. For many projects that don't rely only on Foundation, this isn't a problem - Swift on Linux supports these just fine, and there are instructions out there to handle this. Even better, the creators of Vapor have an excellent service they offer, Vapor Red, which I prefer to handle sites that don't require a macOS host. But what if you want to take advantage of the many frameworks available only on macOS?
This assumes you have the developer tools on your host Mac, but little else. On the host Mac, install Homebrew:
Once installed, pull in Vapor:
Clone your vapor project to wherever it is you want it to run from, then change your directory to your vapor project and build:
Run your project:
You need to make sure that your site starts up on boot. For this, I use supervisord, which you can install using homebrew:
Create the supervisord configuration file:
Open a text editor, and enter the following for the yourapp.ini file:
Make sure that whatever directory you choose to put your log files in, that it exists before you start supervisord. Then, start the supervisord service:
The next thing to do is to create a proxy which passes traffic from the http port (80) and, optionally, the https port (443), to the running application. To do so, first ensure that apache isn't already running on your host:
Then go ahead and install nginx (also fomr homebrew):
Set up nginx:
Modify the “user” on the first line:
Comment out the existing server block (or remove it).
Create a new server in the servers directory:
Set up a new server block that redirects all traffic on port 80 to port 8080 (where your vapor project is running):
Once this is complete, just restart nginx and you should be able to browse to your server on port 80:
I’d recommend setting up SSL for whatever you’re doing, certbot works great for this. Instructions are available on their website.
Other pertinent references that might help: