DEV Community

Yongchang He
Yongchang He

Posted on • Updated on

Setup a Nginx Webserver in 5 minutes

This demo is based on: Amazon Linux 2 AMI (AWS EC2)

The first step is to turn yourself as Root user and upgrade your EC2 instance:

sudo su
yum upgrade 
Enter fullscreen mode Exit fullscreen mode

The two most popular program to run web server are Apache and Nginx. In this tutorial I will use Nginx to show the steps.

I masked my IP address as ADDRESS for security reasons.

[root@ip-ADDRESS ec2-user]# yum install nginx
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
No package nginx available.
Error: Nothing to do
nginx is available in Amazon Linux Extra topic "nginx1"

To use, run
# sudo amazon-linux-extras install nginx1

Learn more at
https://aws.amazon.com/amazon-linux-2/faqs/#Amazon_Linux_Extras

Enter fullscreen mode Exit fullscreen mode

Then run the command as Amazon recommended:

[root@ip-ADDRESS ec2-user]# sudo amazon-linux-extras install nginx1
Enter fullscreen mode Exit fullscreen mode

If the installation is completed successfully, the next step is to run the Nginx.

But before running the Nginx, let's try read the initial web content of Nginx using curl command:

[root@ip-ADDRESS ec2-user]#curl localhost:80
Enter fullscreen mode Exit fullscreen mode

When the Nginx service is stopped, you will get the following message:

[root@ip-ADDRESS ec2-user]#  curl localhost:80
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused
Enter fullscreen mode Exit fullscreen mode

So, let's start the Nginx service:

[root@ip-ADDRESS ec2-user]# service nginx start
Redirecting to /bin/systemctl start nginx.service
Enter fullscreen mode Exit fullscreen mode

And then, type the following command:

[root@ip-ADDRESS ec2-user]# curl localhost:80
Enter fullscreen mode Exit fullscreen mode

You will get:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>Test Page for the Nginx HTTP Server on Amazon Linux</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css">
            /*<![CDATA[*/
            body {
                background-color: #fff;
                color: #000;
                font-size: 0.9em;
                font-family: sans-serif,helvetica;
                margin: 0;
                padding: 0;
            }
            :link {
                color: #c00;
            }
            :visited {
                color: #c00;
            }
            a:hover {
                color: #f50;
            }
            h1 {
                text-align: center;
                margin: 0;
                padding: 0.6em 2em 0.4em;
                background-color: #294172;
                color: #fff;
                font-weight: normal;
                font-size: 1.75em;
                border-bottom: 2px solid #000;
            }
            h1 strong {
                font-weight: bold;
                font-size: 1.5em;
            }
            h2 {
                text-align: center;
                background-color: #3C6EB4;
                font-size: 1.1em;
                font-weight: bold;
                color: #fff;
                margin: 0;
                padding: 0.5em;
                border-bottom: 2px solid #294172;
            }
            hr {
                display: none;
            }
            .content {
                padding: 1em 5em;
            }
            .alert {
                border: 2px solid #000;
            }

            img {
                border: 2px solid #fff;
                padding: 2px;
                margin: 2px;
            }
            a:hover img {
                border: 2px solid #294172;
            }
            .logos {
                margin: 1em;
                text-align: center;
            }
            /*]]>*/
        </style>
    </head>

    <body>
        <h1>Welcome to <strong>nginx</strong> on Amazon Linux!</h1>

        <div class="content">
            <p>This page is used to test the proper operation of the
            <strong>nginx</strong> HTTP server after it has been
            installed. If you can read this page, it means that the
            web server installed at this site is working
            properly.</p>

            <div class="alert">
                <h2>Website Administrator</h2>
                <div class="content">
                    <p>This is the default <tt>index.html</tt> page that
                    is distributed with <strong>nginx</strong> on
                     Amazon Linux.  It is located in
                    <tt>/usr/share/nginx/html</tt>.</p>

                    <p>You should now put your content in a location of
                    your choice and edit the <tt>root</tt> configuration
                    directive in the <strong>nginx</strong>
                    configuration file
                    <tt>/etc/nginx/nginx.conf</tt>.</p>

                </div>
            </div>

            <div class="logos">
                <a href="http://nginx.net/"><img
                    src="nginx-logo.png" 
                    alt="[ Powered by nginx ]"
                    width="121" height="32" /></a>
            </div>
        </div>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

If you fail to start Nginx service, it might be that the hosting provider preinstalled Apache on your instance by default during a fresh install. Just run the following commands:

sudo fuser -k 80/tcp
sudo fuser -k 443/tcp
Enter fullscreen mode Exit fullscreen mode

Then it might be OK to restart it:

sudo service nginx restart
Enter fullscreen mode Exit fullscreen mode

By now, if you paste your public ip address in your browser, you still cannot see the index Nginx webpage. This is because you haven't open the 80 port in your AWS Security Group.

Now edit inbound rules, and add 80 port to your security groups.

The last step, to refresh your web browser again which you typed your public ip address of your EC2 instance, and you will get the welcome page of Nginx.

Welcome to Nginx on Amazon Linux.

Top comments (0)