Learn How To Setup a Meteor.js Web Application With MongoDB and Apache on Ubuntu 16.04 LTS
Table of Contents
- Installing Node.js
- Installing MongoDB
- Installing and setting up Apache2
- Installing Meteor.js & creating our base application
- Setting up our Apache reverse proxy
- Running the site for the first time
- Setting up our website for production
- Managing our application
Meteor.js is an open-source, JavaScript framework written with Node.js in mind. It is popular because of its view of building web applications in pure JavaScript. Meteor incorporates front-end JavaScript support that runs in the web browser, as well as back-end JavaScript, which would run on the host server. It integrates very well with front-end JavaScript frameworks, such as React and Angular.js, as well as MongoDB, a popular NoSQL database. A few big reasons to choose Meteor.js are as follows:
-
Meteor.js is a full stack framework. So with it, you get everything you could ever need: Database, front end, backend. Meteor does an amazing job of tying it all together.
-
Speed. Meteor’s intuitive package system gives you many tools that you may need to perfect your application.
-
It has good support from the Meteor Developer Group, as well as fellow developers who use it. You’ll easily be able to figure out any problems you may have and resolve them quickly.
In this tutorial, we will accomplish the following goals on Ubuntu 16.04:
-
Install
Node.js
, our Javascript runtime. -
Install
Meteor.js
. -
Install
MongoDB
, Meteor’s database of choice. -
Install
Apache
, our reverse proxy of choice, and set up our domain if applicable. -
Test our website.
-
Setup our website for production.
Installing Node.js
You’ll first need to install Node.js, which is our JavaScript interpreter, and Meteor’s core. It will allow us to run Meteor, and other Node.js applications we may have in the future. In this situation, we will be installing Node.js 8. To start the download, simply run the following in your terminal.
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
Wait for the download and installation to complete.
Installing MongoDB
Next, we will be installing our database: MongoDB. MongoDB is a free, open-source NoSQL document database, and Meteor’s database of choice. It uses a JSON-like format for its documents, as opposed to structured tables in a traditional SQL database.
Import the MongoDB public key used by APT
(Advanced Packaging Terminal). This allows APT to verify the package; in this case, MongoDB.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Create the necessary list file for Ubuntu 16.04.
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Start the installation of MongoDB, and wait for the installation to finish.
sudo apt update && sudo apt install mongodb-org -y
Open the systemd service for editing.
sudo nano /etc/systemd/system/mongodb.service
Copy and paste the following to complete the systemd service.
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
Use “Control-O
” to save, and “Control-X
” to exit.
Run the MongoDB systemd service by typing sudo systemctl start mongodb
in your terminal.
To verify that it has started successfully, type the following.
sudo systemctl status mongodb
You will see that the service is active.
Installing and setting up Apache2
Next, we will install Apache. Apache is a free and open source web server software, which also functions as a reverse proxy, which is what we will be utilizing in this tutorial. A reverse proxy is needed to bind our Meteor.js Application to port 80
. Node.js blocks applications from running on that port without root access. Apache runs as root
automatically as well as binds to port 80
, so we won’t have to worry about it when it comes time to run our website.
Install Apache.
sudo apt update && sudo apt install apache2
In order to allow access to outside web ports such as port 80
, we need to configure our firewall for Apache. We do this through UFW
(Uncomplicated Firewall).
sudo ufw allow 'Apache Full'
This will allow access to the “Apache Full
” profile. This gives us the incoming traffic to port 80
, which is what our application will be running on.
Installing Meteor.js & creating our base application
Now, we will install our web framework: Meteor.js. We will use a simple bash script provided by the Meteor team to install it.
curl https://install.meteor.com/ | sh
Wait for the installation to finish. Basically what the script does is it downloads Meteor from the official website, and installs it globally, so we can use it from anywhere. Once it is finished, we can create our application directory. Meteor has a handy little tool for us to use to do that, called meteor create
. To create your application directory, type the following.
meteor create <projectname>
The folder will be created with the name specified (<projectname>
).
Setting up our Apache reverse proxy
Now that we have our application directory set up, we can proceed with setting up our reverse proxy. Apache uses a module called mod_proxy
, which implements a proxy for Apache.
sudo apt-get install libapache2-mod-proxy-html libxml2-dev -y
This will install the module and make it available for Apache to use.
Next, we will need to enable all of the necessary modules Apache needs to run. We will do this with a2enmod
, a tool that enables modules for Apache. These modules will allow us to take advantage of the reverse proxy. Simply type these commands into your terminal.
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_ajp
sudo a2enmod rewrite
sudo a2enmod deflate
sudo a2enmod headers
sudo a2enmod proxy_balancer
sudo a2enmod proxy_connect
sudo a2enmod proxy_html
After that, we will need to disable the default Apache site from starting up, so we can start up our own. Otherwise, the default Apache application will override ours. To disable the default site, simply run the following.
sudo a2dissite 000-default
Now we will create our virtual host file. To do that, just open a text editor.
sudo nano /etc/apache2/sites-available/<projectname>
Copy and paste the following.
<VirtualHost *:80>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPreserveHost On
# Servers to proxy the connection, or
# List of application servers Usage
ProxyPass / http://0.0.0.0:3000/
ServerName localhost
</VirtualHost>
-
VirtualHost *:80
: Tells Apache to attach to port 80, which is what we want for our web application. -
ProxyPass
: The IP address of the site you want to forward to the reverse proxy. This will most likely be the IP of your VPS -
ServerName
: The name of your server (the default name is usuallylocalhost
).
Once you are all done configuring the necessary settings, use “Control-O
” to save and “Control-X
” to exit.
Running the site for the first time
To test and make sure that the website is running, type the following in the project directory.
meteor
You will see the following output showing that your site has started successfully.
=> App running at: http://localhost:3000/
Take note that Meteor, by default listens on port 3000
.
Setting up our website for production
In order to make sure our app is able to stay running, we will use a systemd service. Similar to what we did above with MongoDB, this system service will make sure that our website will start up whenever our system starts, and stay running. It will also restart our application, in the event that it crashes for whatever reason. To setup our systemd service, type the following in your terminal.
sudo nano /etc/systemd/system/<projectname>.service
Here is what you want your file to look like.
[Service]
WorkingDirectory=/home/<yourusername>/<projectname>
ExecStart=/usr/local/bin/meteor --production
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<projectName>
User=<yourusername>
Environment=NODE_ENV=production
Environment=PWD=/home/<yourusername>/<projectname>
Environment=PORT=3000
Environment=HTTP_FORWARDED_COUNT=1
Note: Don’t forget to replace projectname
with the name of the project, and yourusername
with the username of the VPS.
Here are some key lines for you to keep in mind.
-
WorkingDirectory
: The directory of your application. -
Restart
: Whether or not to restart the application of it stops for whatever reason. -
User
: Your username. -
Environment=PWD
: Identical toWorkingDirectory
. -
Environment=PORT
: The port that your application is running on. The default is3000
.
Save and close the file.
Now, we will enable and start the service.
sudo systemctl enable <projectname>.service
Where <projectname>
is the name of the service file we created.
Then, we will start the service.
sudo systemctl start <projectname>.service
Your application will start. To verify that it has launched, just run the following.
sudo systemctl status <projectname>
You will see that it is active, verifying that the service has started successfully.
Next, we will check out our website. In your web browser of choice, navigate to your IP address.
http://your-server-ip/
You will see the Meteor sample screen, verifying that you have done everything correctly.
Managing our application
Now that we have started our application, we will need to manage it.
Restarting your application
sudo systemctl restart <projectname>
Stopping the application
sudo systemctl stop <projectname>
Viewing the application’s status
sudo systemctl status <projectname>
Viewing the logs
journalctl -u <projectname>
You have now successfully configured MongoDB, Apache, and Meteor, and created a Meteor.js web server for production. Now the rest is up to you, to design your website on the front end and the back end. You have access to MongoDB to store any data you may need, and Node.js, which offers a variety of modules available through Node Package Manager (NPM
) to further strengthen your backend. For more documentation, feel free to visit the Meteor site, where you can further learn how to customize your website to your liking. Also, you may refer to the MongoDB documentation, when you are dealing with database operations.
Need help?
Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!