People's interest in functional programming is increasing, and more specifically, programming the Web in Clojure. Many tutorials on how to build basic applications often ignore deployment details. This article will show you how to deploy Clojure web applications to Ubuntu 14.04 CVM.
Specifically, we will create a sample Clojure application and package it for production use, and use Supervisor to set up the Clojure application environment on the server to run the application and Nginx to provide requests to it.
Before starting this tutorial, you need to prepare the following:
The first step is to use git
to grab the sample Clojure project for deployment.
First, update the package and install git
on the server.
sudo apt-get update
sudo apt-get install git
Next, clone the sample project repository.
git clone https://github.com/do-community/do-clojure-web.git
This repository is the final result of following the Clojure Basic Web Development tutorial. If you want, you can follow the tutorial yourself instead of cloning this repository.
Clojure uses JVM to run its code, so you need to compile the project to run it. Leiningen is a dependency management and build automation tool for Clojure applications, which can be easily implemented. There are two steps to establish Leiningen.
First, install Java.
sudo apt-get install openjdk-7-jre-headless
Next download the Leiningen installation script. Leiningen has an Ubuntu package, but it is outdated.
sudo curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein -o /usr/local/bin/lein
Set permissions so that any user can use the lein
utility provided by Leiningen.
sudo chmod a+x /usr/local/bin/lein
Now you can compile your project to run lein
on the server.
cd ~/do-clojure-web
lein uberjar
We need three main parts to make this application work: Java, Supervisor and Nginx. We have just installed Java, next, we will install Supervisor and Nginx.
sudo apt-get install nginx supervisor
You also need a place to save your Clojure web application and its log files. Next create the directory structure.
sudo mkdir -p /var/www/do-clojure-web/app/db /var/www/logs
Now you can move the Clojure application files and database files to the directory you created.
sudo cp ~/do-clojure-web/target/do-clojure-web-0.1.0-standalone.jar /var/www/do-clojure-web/app/
sudo cp ~/do-clojure-web/db/do-clojure-web.h2.db /var/www/do-clojure-web/app/db/
The application will run as the user www-data on the system so that it can write to our built-in database. Set the owner of the application path to www-data.
sudo chown -R www-data /var/www/do-clojure-web/
Switch to the Clojure application directory.
cd /var/www/do-clojure-web/app/
In a production environment, the version number of the application will change with each update. You don't want to update the system configuration every time. To prevent this, create a symbolic link for the currently running application version. You will refer to symbolic links in the next steps.
sudo ln -s do-clojure-web-0.1.0-standalone.jar do-clojure-web.jar
The application is currently configured to be accessible only via localhost, but you can still be sure that it launches without errors. Do this before continuing.
sudo java -jar do-clojure-web.jar
If everything is ok, you should get output similar to this:
...2015- 06- 1204:30:17.882: INFO:oejs.Server:jetty-7.x.y-SNAPSHOT
2015- 06- 1204:30:17.995: INFO:oejs.AbstractConnector:Started [email protected]:5000
Press the key combination CTRL+C
to stop the application immediately.
There are several options to manage the application as a service. The option you use here is called Supervisor; it is easier to manage and more versatile than simple scripts. However, for services that really need to be extended, please check the uWSGI documentation about running Clojure applications.
Create and edit the /etc/supervisor/conf.d/do-clojure-web.conf
file.
sudo nano /etc/supervisor/conf.d/do-clojure-web.conf
Add this configuration to the file and save it.
[ program:do-clojure-web]
command=/usr/bin/java -jar do-clojure-web.jar
directory=/var/www/do-clojure-web/app
user=www-data
autostart=true
autorestart=true
startretries=3
redirect_stderr=true
stdout_logfile=/var/www/logs/do-clojure-web.app.log
This configuration is very simple. The Supervisor daemon (service) will run our application from the /var/www/do-clojure-web/app
directory. If /var/www/logs/do-clojure-web.app.log
crashes, it will also make sure to log in and try to restart the application.
Because the Clojure web application only accepts connections from localhost on port 5000, we need to place a web server like Nginx in front of it to provide external access. This is also very convenient for providing static assets when extending the application.
Edit the /etc/nginx/sites-available/default
file.
sudo nano /etc/nginx/sites-available/default
Add the commented code block part to the file. This defines our backend for reference in the next configuration section.
...
# Please see /usr/share/doc/nginx-doc/examples/for more detailed examples.
##
upstream http_backend {//Add to file
server 127.0.0.1:5000;
keepalive 32;}
server {
listen 80 default_server;...
Now find the block beginning with location /
. Comment out all lines by adding a #
at the beginning of each line.
...
# Make site accessible from http://localhost/
server_name localhost;
# location /{
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/=404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# }
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
...
Then, add the following section below, which will tell Nginx to listen like a normal web server on port 80 and proxy your request to the Clojure application.
...
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
location /{
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
access_log /var/www/logs/do-clojure-web.access.log;
error_log /var/www/logs/do-clojure-web.error.log;}
# location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
It is time to start all the work and make sure everything is in order. Go ahead and start the Supervisor daemon so that your Clojure application starts.
sudo service supervisor start
Wait for about 30 seconds to start, and then start the Nginx web server front-end proxy.
sudo service nginx start
Visit http://your_server_ip
in your browser. You should see the sample Clojure application site load.
If you just get the default Nginx page, please try to restart Supervisor with sudo service supervisor restart
, wait 30 seconds, and then restart Nginx with sudo service nginx restart
.
After loading the website, click the "Add Location" link at the top of the screen and try to add some digital coordinates to ensure that the database access permissions are correct. For example, you can add 1 for the x value and 2 for the y value. This will bring you to a page saying:
Added [1,2](id:1) to the db. See for yourself.
If you click the "View all locations" link at the top of the screen, you should see a table with new entries.
You have just deployed a Clojure application using Leiningen, Supervisor and Nginx! There is still a lot to learn about the topic of deploying even the simplest websites and applications. The next step is to deploy the custom application, but not the sample application used in this tutorial.
To learn more about the Linux open source information tutorial, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Deploy a Clojure Web Application on Ubuntu 14.04"
Recommended Posts