{"id":4491,"date":"2023-07-03T14:26:00","date_gmt":"2023-07-03T14:26:00","guid":{"rendered":"https:\/\/hostiko.com\/layout81\/?p=4491"},"modified":"2026-11-21T18:43:41","modified_gmt":"2026-11-21T18:43:41","slug":"how-to-install-nginx-on-ubuntu","status":"publish","type":"post","link":"https:\/\/cloudvpshosts.com\/kb\/how-to-install-nginx-on-ubuntu\/","title":{"rendered":"How to Install Nginx on Ubuntu 24.04 and 22.04"},"content":{"rendered":"<p>To install Nginx on Ubuntu, update your package lists, run <code>sudo apt install nginx<\/code>, allow HTTP and HTTPS through the UFW firewall, and confirm the service is running with <code>systemctl status nginx<\/code>. You can then serve your own site by creating a document root and a server block for your domain. The whole setup takes about ten minutes.<\/p>\n<p>This guide is for Ubuntu 24.04 LTS and Ubuntu 22.04 LTS on a Cloud VPS Hosts VPS or dedicated server. The same commands work on Debian 12. You need SSH access as a user with sudo rights.<\/p>\n<h2>What do you need before you install Nginx?<\/h2>\n<p>You need a fresh Ubuntu server you can reach over SSH, and optionally a domain name that points to it.<\/p>\n<ul>\n<li>A server running Ubuntu 24.04 or 22.04. If you have not prepared it yet, follow our <a href=\"https:\/\/cloudvpshosts.com\/kb\/initial-server-setup-with-ubuntu\/\">initial Ubuntu server setup guide<\/a> first.<\/li>\n<li>A user with sudo rights and SSH key login. See <a href=\"https:\/\/cloudvpshosts.com\/kb\/setting-up-an-ssh-key\/\">Setting up an SSH key<\/a>.<\/li>\n<li>For a real website, a domain with an A record (and an AAAA record if you use IPv6) pointing to your server&#8217;s IP address.<\/li>\n<\/ul>\n<p>The examples use <code>example.com<\/code> as the domain and <code>203.0.113.10<\/code> as the server IP. Replace both with your own values.<\/p>\n<h2>How do you install Nginx on Ubuntu?<\/h2>\n<p>Nginx is in Ubuntu&#8217;s default repositories, so one <code>apt<\/code> command installs it and starts the service.<\/p>\n<pre><code>sudo apt update\r\nsudo apt install nginx -y<\/code><\/pre>\n<p>Ubuntu starts Nginx automatically and enables it at boot. Check the installed version with:<\/p>\n<pre><code>nginx -v<\/code><\/pre>\n<p>Ubuntu 24.04 ships Nginx 1.24 and Ubuntu 22.04 ships Nginx 1.18. Both are maintained with security updates through Ubuntu&#8217;s normal update process.<\/p>\n<h2>How do you allow Nginx through the firewall?<\/h2>\n<p>If UFW is active, allow both HTTP and HTTPS with the &#8220;Nginx Full&#8221; profile, which opens ports 80 and 443.<\/p>\n<pre><code>sudo ufw app list\r\nsudo ufw allow 'Nginx Full'\r\nsudo ufw status<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>UFW profile<\/th>\n<th>Ports opened<\/th>\n<th>When to use it<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Nginx HTTP<\/td>\n<td>80<\/td>\n<td>Testing only, before you add SSL<\/td>\n<\/tr>\n<tr>\n<td>Nginx HTTPS<\/td>\n<td>443<\/td>\n<td>HTTPS only, if another rule handles port 80<\/td>\n<\/tr>\n<tr>\n<td>Nginx Full<\/td>\n<td>80 and 443<\/td>\n<td>Most websites, including the HTTP to HTTPS redirect<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Make sure OpenSSH is still allowed before you enable UFW, or you will lock yourself out of the server. <code>sudo ufw status<\/code> should list <code>OpenSSH<\/code> as ALLOW.<\/p>\n<h2>How do you check that Nginx is running?<\/h2>\n<p>Ask systemd for the service status:<\/p>\n<pre><code>systemctl status nginx<\/code><\/pre>\n<p>Look for <code>Active: active (running)<\/code> in the output. Press <code>q<\/code> to exit. Then open <code>http:\/\/203.0.113.10<\/code> in your browser. You should see the default &#8220;Welcome to nginx!&#8221; page, which confirms the web server answers on port 80.<\/p>\n<p>These commands manage the service from now on:<\/p>\n<table>\n<thead>\n<tr>\n<th>Task<\/th>\n<th>Command<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Apply configuration changes without dropping connections<\/td>\n<td><code>sudo systemctl reload nginx<\/code><\/td>\n<\/tr>\n<tr>\n<td>Restart the service<\/td>\n<td><code>sudo systemctl restart nginx<\/code><\/td>\n<\/tr>\n<tr>\n<td>Stop or start<\/td>\n<td><code>sudo systemctl stop nginx<\/code> \/ <code>sudo systemctl start nginx<\/code><\/td>\n<\/tr>\n<tr>\n<td>Test the configuration for errors<\/td>\n<td><code>sudo nginx -t<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>How do you host your own website with an Nginx server block?<\/h2>\n<p>A server block tells Nginx which folder to serve for which domain. Create a folder for your site, add a page, write the server block and enable it.<\/p>\n<h3>1. Create the document root<\/h3>\n<pre><code>sudo mkdir -p \/var\/www\/example.com\/html\r\nsudo chown -R $USER:$USER \/var\/www\/example.com\/html<\/code><\/pre>\n<h3>2. Add a test page<\/h3>\n<pre><code>nano \/var\/www\/example.com\/html\/index.html<\/code><\/pre>\n<p>Paste a minimal page and save it:<\/p>\n<pre><code>&lt;!doctype html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n  &lt;head&gt;&lt;title&gt;example.com is working&lt;\/title&gt;&lt;\/head&gt;\r\n  &lt;body&gt;&lt;h1&gt;Nginx is serving example.com&lt;\/h1&gt;&lt;\/body&gt;\r\n&lt;\/html&gt;<\/code><\/pre>\n<h3>3. Create the server block<\/h3>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/example.com<\/code><\/pre>\n<pre><code>server {\r\n    listen 80;\r\n    listen [::]:80;\r\n\r\n    server_name example.com www.example.com;\r\n    root \/var\/www\/example.com\/html;\r\n    index index.html;\r\n\r\n    location \/ {\r\n        try_files $uri $uri\/ =404;\r\n    }\r\n}<\/code><\/pre>\n<h3>4. Enable the site, test and reload<\/h3>\n<pre><code>sudo ln -s \/etc\/nginx\/sites-available\/example.com \/etc\/nginx\/sites-enabled\/\r\nsudo nginx -t\r\nsudo systemctl reload nginx<\/code><\/pre>\n<p>Reload only after <code>nginx -t<\/code> reports &#8220;syntax is ok&#8221; and &#8220;test is successful&#8221;. Now open <code>http:\/\/example.com<\/code> and you should see your test page.<\/p>\n<p>If <code>nginx -t<\/code> reports <code>could not build server_names_hash<\/code>, you are using long domain names. Open <code>\/etc\/nginx\/nginx.conf<\/code>, remove the <code>#<\/code> in front of <code>server_names_hash_bucket_size 64;<\/code>, then test and reload again.<\/p>\n<h2>How do you secure the site with HTTPS?<\/h2>\n<p>Once the site works on HTTP, add an SSL certificate so browsers show the padlock. Our <a href=\"https:\/\/cloudvpshosts.com\/kb\/how-to-install-an-ssl-certificate-on-nginx-ubuntu-debian\/\">guide to installing an SSL certificate on Nginx<\/a> picks up exactly where this one ends, starting from the server block above. You will need a CSR first, covered in <a href=\"https:\/\/cloudvpshosts.com\/kb\/how-to-generate-a-csr-for-your-ssl-certificate\/\">How to generate a CSR<\/a>.<\/p>\n<p>Paid RapidSSL, GeoTrust and DigiCert certificates, including OV, EV and Wildcard options, are available in our <a href=\"https:\/\/cloudvpshosts.com\/ClientArea\/index.php?rp=\/store\/ssl-certificates\">SSL certificate store<\/a>.<\/p>\n<h2>Where are Nginx&#8217;s important files on Ubuntu?<\/h2>\n<table>\n<thead>\n<tr>\n<th>Path<\/th>\n<th>What it holds<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>\/etc\/nginx\/nginx.conf<\/code><\/td>\n<td>Main configuration file<\/td>\n<\/tr>\n<tr>\n<td><code>\/etc\/nginx\/sites-available\/<\/code><\/td>\n<td>Server blocks for each site<\/td>\n<\/tr>\n<tr>\n<td><code>\/etc\/nginx\/sites-enabled\/<\/code><\/td>\n<td>Links to the server blocks Nginx actually loads<\/td>\n<\/tr>\n<tr>\n<td><code>\/var\/www\/<\/code><\/td>\n<td>Website files<\/td>\n<\/tr>\n<tr>\n<td><code>\/var\/log\/nginx\/access.log<\/code><\/td>\n<td>Every request Nginx serves<\/td>\n<\/tr>\n<tr>\n<td><code>\/var\/log\/nginx\/error.log<\/code><\/td>\n<td>Errors, the first place to look when something fails<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Need a server to follow along? Our <a href=\"https:\/\/cloudvpshosts.com\/linux-vps.php\">Linux VPS plans<\/a> come with full root access and support Ubuntu 22.04 and 24.04.<\/p>\n<h2>Frequently asked questions<\/h2>\n<h3>Should I install Nginx from Ubuntu&#8217;s repository or from nginx.org?<\/h3>\n<p>Ubuntu&#8217;s repository is the simplest choice and receives security fixes through normal system updates. Use the official nginx.org packages only if you need features from a newer Nginx release.<\/p>\n<h3>How do I remove the default Nginx welcome page?<\/h3>\n<p>Remove the default site link with <code>sudo rm \/etc\/nginx\/sites-enabled\/default<\/code>, then run <code>sudo nginx -t<\/code> and reload Nginx. Your own server blocks keep working.<\/p>\n<h3>Why do I get a 403 Forbidden error?<\/h3>\n<p>Nginx cannot read your files or there is no index file. Check that <code>index.html<\/code> exists in the folder set as <code>root<\/code>, and that the folders are readable, for example with <code>sudo chmod -R 755 \/var\/www\/example.com<\/code>.<\/p>\n<h3>Can I run Nginx and Apache on the same server?<\/h3>\n<p>Not on the same ports. Only one program can listen on port 80 and 443. If Apache is installed, stop and disable it with <code>sudo systemctl disable --now apache2<\/code> before starting Nginx.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install Nginx on Ubuntu 24.04 or 22.04, open the firewall, check the service and host your own site with a server block.<\/p>\n","protected":false},"author":1,"featured_media":8034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"","_yoast_wpseo_focuskw":"","rank_math_title":"How to Install Nginx on Ubuntu 24.04 and 22.04 | CVH","rank_math_description":"Install Nginx on Ubuntu 24.04 or 22.04 step by step: apt install, UFW firewall rules, service checks and a server block for your own domain.","rank_math_focus_keyword":"install Nginx on Ubuntu"},"categories":[69],"tags":[226,246,244,240,227],"class_list":["post-4491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-linux","tag-linux-vps","tag-nginx","tag-tutorial","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/posts\/4491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/comments?post=4491"}],"version-history":[{"count":4,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/posts\/4491\/revisions"}],"predecessor-version":[{"id":8333,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/posts\/4491\/revisions\/8333"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/media\/8034"}],"wp:attachment":[{"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/media?parent=4491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/categories?post=4491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudvpshosts.com\/kb\/wp-json\/wp\/v2\/tags?post=4491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}