Setting up GraphQL server on Ubuntu

Setting up GraphQL server can be a daunting task if you don’t know the  steps, because, lot’s of time goes into trial and error to find out the exact steps. So here I am going to jot down the steps need to set up GraphQL on Ubuntu server.

I will be using Mysql as the database and thus I will also install Phpmyadmin to manage the database. To install  Phpmyadmin we either need LAMP stack or LEMP stack. We will go for LEMP stack because we might need Nginx as reverse proxy for our Node application.

I am using Digital Ocean and it has one click install of LEMP server, so, I will install LEMP using one click install feature. You can find out newly installed MySql password by using command below :

cat ~/.digitalocean_password

Next, I will stall phpmyadmin using the command below :

sudo apt-get update
sudo apt-get install phpmyadmin php-mbstring php-gettext

And enable the PHP mcrypt and mbstring extensions using the command below :

sudo phpenmod mcrypt
sudo phpenmod mbstring

Now, we need to create symbolic link of phpmyadmin we just installed, in nginx publicly accessible directory, so that nginx web server can find phpmyadmin. Using the command below we create phpmyadmin symbolic link :

sudo ln -s /usr/share/phpmyadmin /var/www/html

So we have completed phpmyadmin installation. You can check installation by opening the url http://dropletipaddress/phpmyadmin, where you need to replace your droplet’s ip address with ‘dropletipaddress’ in the url .  To login enter ‘root’ as username and MySql password that we found above.

Next, we will install Nodejs using the command below :

cd ~
curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs

In the above command instead of 9.x you can use the latest available node js version. To check the version of nodejs that has been installed use the command below:

nodejs -v

Next, we will install PM2, Nodejs application process manager, that will run our node app in background. Use the command below to install PM2 :

sudo npm install -g pm2

Next, we will open a port on Ubuntu to run our Node application using the command below :

iptables -A INPUT -p tcp --dport 4000 -j ACCEPT

So, I have opened port 4000 and we can run our app on port 4000 and access it.

Now, we can transfer our GraphQL app on server and run it with the command below :

pm2 start npm -- start

To access graphiql and test some queries go to following url :

http://dropletipaddress:4000/graphiql

In the above url you need to replace your droplet’s IP address with ‘dropletipaddress‘.

Video Tutorial