Back to Course |
Deploy Laravel Project to AWS EC2: Step-By-Step

Install NginX HTTP server

We need to install a web-server to our EC2 server, to actually serve our web-project.

  1. To install NginX enter:
root@ip-172-31-44-101:~# apt-get install nginx
  1. Check the status of the NginX server using systemctl status nginx:
root@ip-172-31-44-101:~# systemctl status nginx
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-11-09 17:18:47 UTC; 13min ago
Docs: man:nginx(8)
Main PID: 1679 (nginx)
Tasks: 2 (limit: 1143)
Memory: 1.7M
CPU: 20ms
CGroup: /system.slice/nginx.service
├─1679 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─1680 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Launch NginX on boot

If service is enabled:

on 2nd line we see:

Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

enabled; means that the nginx service starts on boot and vendor preset: enabled means that by default it is enabled, so no changes are needed.

If service is disabled:

Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled)

In case it says disabled; run systemctl enable nginx to start nginx on boot.

root@ip-172-31-44-101:~# systemctl enable nginx
Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

Start NginX now

NginX is running:

3rd line is also in our interest:

Active: active (running) since Wed 2022-11-09 17:18:47 UTC; 13min ago

This means the server is actually running and no further action is required.

Nginx is dead:

Active: inactive (dead) since Wed 2022-11-09 17:36:17 UTC; 1s ago

If the nginx server is not started, run systemctl start nginx and check the status again systemctl status nginx:

root@ip-172-31-44-101:~# systemctl start nginx
  1. Optionally if you're interested to see what processes are bound to each port run lsof -i -P -n | grep LISTEN. This might be handy in the future. The output should be similar to that:
root@ip-172-31-44-101:~# lsof -i -P -n | grep LISTEN
systemd-r 410 systemd-resolve 14u IPv4 16617 0t0 TCP 127.0.0.53:53 (LISTEN)
sshd 766 root 3u IPv4 18688 0t0 TCP *:22 (LISTEN)
sshd 766 root 4u IPv6 18699 0t0 TCP *:22 (LISTEN)
nginx 2345 root 6u IPv4 27464 0t0 TCP *:80 (LISTEN)
nginx 2345 root 7u IPv6 27465 0t0 TCP *:80 (LISTEN)
nginx 2346 www-data 6u IPv4 27464 0t0 TCP *:80 (LISTEN)
nginx 2346 www-data 7u IPv6 27465 0t0 TCP *:80 (LISTEN)
  1. Verify that the server is reachable: enter the server's IP address in the browser http://18.195.117.231/.

If you forgot or didn't save the public IP from earlier, you can find it directly in the terminal by entering:

root@ip-172-31-44-101:~# wget -qO- icanhazip.com
18.195.117.231

Note: protocol here is http:// and not https:// because we have no services listening on port 443 (https) as we checked with the previous command lsof -i -P -n | grep LISTEN

You should see something like this:

NginX running

The default nginx configuration file is located at /etc/nginx/sites-enabled/default -> /etc/nginx/sites-available/default.

root@ip-172-31-44-101:/etc/nginx/sites-enabled# cat /etc/nginx/sites-enabled/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
 
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
 
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
 
root /var/www/html;
 
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
 
server_name _;
 
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
 
# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}
 
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
 
 
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

We only have document root defined as /var/www/html and our default file we seen in browser is index.nginx-debian.html.

root@ip-172-31-44-101:/etc/nginx/sites-enabled# cat /var/www/html/index.nginx-debian.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
 
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
 
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Let's leave it for now and install PHP and other dependencies.