We need to install a web-server to our EC2 server, to actually serve our web-project.
- To install NginX enter:
root@ip-172-31-44-101:~# apt-get install nginx
- 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 nginxSynchronizing 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
- 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 LISTENsystemd-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)
- 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.com18.195.117.231
Note: protocol here is
http://
and nothttps://
because we have no services listening on port 443 (https) as we checked with the previous commandlsof -i -P -n | grep LISTEN
You should see something like this:
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 andworking. 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.
Thanks for this amazing tutorial.. when run this command : apt-get install nginx, have an error E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
You probably aren't logged in as root, this is covered in previous lessons about SSH connection and logging in, from what I remember.
I runed all commands of last lectures and no error but this command the same problem
This is not a very serious error.
This error usually occurs when a process is functioning to update the system and you try to access the for some other operation. This can be checked if any of apt processes are running by entering
ps aux | grep -i apt
. If that is the case you should wait until the process is finished and try again, this is common during system updates, note that security updates are being installed automatically.Another solution might be to reboot the server
sudo reboot
, and try installing nginx again.If it does not solve the issue try to kill the process with
sudo kill <process id>
. Process ids are shown using the previousps aux | grep -i apt
command.Sometimes the issue may come across a more complex situation, and the issue could be the "lock" files. Lock files restrict access to the system files until a specific operation is performed. Upon completion of the process, the lock will automatically be released to perform further operations of the system. So, to solve this issue you are required to delete the files. Firstly check the lock file using
sudo lsof /var/lib/dpkg/lock-frontend
.If you see something "unattended" in the output, wait for this process to complete because the system is working on updates.
Otherwise, get the process ids and terminate them using
sudo kill -9 <process id>
.After that, you can delete the lock file
sudo rm /var/lib/dpkg/lock-frontend
.Make sure you reconfigure "dpkg" after deleting the lock file
sudo dpkg --configure -a
realy thanks you i solved it
so how to get SSL (HTTPS)
Check out Let's Encrypt