We all know how painful it is as a developer to configure a server and make it work properly. But if you are a back end developer, you must know how to deploy your app on the cloud.
Sometimes you have multiple applications that needs to be deployed and you don’t want to waste your money buying multiple servers. Because you already know, servers are expensive!
In that case you need to configure a reverse proxy on your server. There are lot of reverse proxy servers. But nginx is one of the most popular choices and we’ll use nginx to route our requests to different ports.
1. What are we going to deploy today?
Suppose we’ve 2 different applications that needs to be deployed on our cloud instance.
- A java app
- A wordpress site
To deploy anything on your server, first you need access to that server to begin with, right?
Assuming you have a cloud instance (Ubuntu) with ssh access. So ssh to your instance like that
ssh [email protected]
2. Deploy a java app
In one of my previous tutorial I showed you how you can deploy a java app on a cloud instance. Check it out.
3. Deploying a wordpress site
Since wordpress is a php script, so you need php to interpret your php shit.
Install PHP
sudo apt-get install php-fpm php-mysql php-mbstring
install mysql
sudo apt-get install mysql-server
on production environment run this command to ensure mysql installation is secured
sudo mysql_secure_installation
Download wordpress and unzip it on /var/www/wordpress
wget https://wordpress.org/latest.zip
unzip file.zip -d /var/wordpress
4. Install Nginx
Now we’re heading to the good stuff. We’ll install nginx to use it as a reverse proxy server and also to configure wordpress to run on nginx using php.
install nginx
sudo apt-get install nginx
if everything goes well you’ll see nginx running on your server on port 80
5. Configuring Nginx
Now open nginx default configuration file and add some code to it. ( Assuming your java app is running on port 8080 and wordpress files are inside /var/www/wordpress folder )
sudo nano /etc/nginx/sites-available/default
# FOR WORDPRESS (SERVER CONFIGURATION) server { listen 80; listen [::]:80; server_name example.com www.example.com; root /var/www/wordpress; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; } location ~ /\.ht { deny all; } } # REVERSE PROXY CONFIGURATION FOR AN APP RUNNING ON PORT 8080 server { listen 80; listen [::]:80; server_name api.example.com; location / { proxy_pass http://localhost:8080; } }
Here, on our first server{} configuration, we listened to port 80, we defines our server with server_name example.com www.example.com;
so if any request comes on
or example.com
on port 80 it’ll use this configuration to serve that request.www.example.com
With
we showed the location of php files, and root /var/www/wordpress;
index index.php index.html index.htm index.nginx-debian.html;
defines which files will be counted as index file. php stuff!!
On our second server{} configuration, we did listen port 80 with
but added server_name api.example.com;
on our location block. This means nginx will route http requests to the port 8080 if any request comes from proxy_pass http://localhost:8080;
api.example.com
on port 80
Pretty intelligent stuff!
To save it remember to press
and then type Ctrl + X
and then press Y
.enter
Now restart nginx server and you’re good to go
service nginx restart
Oh wait!
If you send a request to the server using ip address it’ll always use the first configuration if no config server_name
matches with that ip.
But we’ll never use ip address, we want to use our domain for that stuff. So you’ve to configure your dns server to point to your server ip address.
And how would you do that?
You silly adorable human being, just add an A record
with that ip on your dns configuration 🙂