Install a nginx web server on docker container

nginx in docker container
Spread the love

To browse the server we need an IP address. Docker will automatically set an IP address for each running container. Docker has special command to get the IP address of a running container

In previous post, we saw how can we run a container from an existing docker image. In this post, we will see how can we install a nginx web server. For this purpose, we are first going to run a container and then install nginx.

First of all lets run a container using Ubuntu:latest

We have a container named nginx_container up and running. Lets get inside the container and get our hand dirty.

Before executing any command, you should keep in mind that this is a base image and it may not packed with all the latest packages. Therefore, you should always update the container and then only run command to install package.

We can now install nginx

A web server is incomplete without PHP. Our second step will be to install PHP package

Nice, we have successfully installed PHP 7.2 we need to setup nginx so that our virtualhost can use the PHP. Check the file /etc/php/7.2/fpm/pool.d/www.conf, at around line number 36 you will see something like below

Note it down. Open file /etc/nginx/sites-enabled/default, change the lines from

to following

You may also want to set index.php as the first page to open. For this change the following line

to

Save the file and quit. As a server administrator, it is a good practice to check the syntax before you restart nginx. Run the following command to check the syntax

Go to /var/www/html and remove everything there. Create new index.php file and insert the php code as done below

For this demo, it is all we require. Now, start these services

To browse the server we need an IP address. Docker will automatically set an IP address for each running container. Docker has special command to get the IP address of a running container. In your next terminal type

At the end of the output you will see something like below and will easily notice the IP address

In our case it is 172.17.0.2. Open your favorite web browser and paste the IP address, you will see

Voila!! That is all for now. Next we will see how can we create a docker image.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *