{"id":16017,"date":"2023-06-21T14:55:54","date_gmt":"2023-06-21T14:55:54","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=16017"},"modified":"2023-09-18T15:52:42","modified_gmt":"2023-09-18T15:52:42","slug":"how-to-install-and-configure-nginx-on-an-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-install-and-configure-nginx-on-an-ubuntu-18-04\/","title":{"rendered":"How to Install and Configure Nginx on Ubuntu 18.04"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-1024x768.jpg\" alt=\"How to Install and Configure Nginx on an Ubuntu 18.04\" width=\"1024\" height=\"768\" class=\"alignnone size-large wp-image-16018 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-1024x768.jpg 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-300x225.jpg 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-1536x1152.jpg 1536w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-2048x1536.jpg 2048w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-128x96.jpg 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-420x315.jpg 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-540x405.jpg 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-720x540.jpg 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-960x720.jpg 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-1140x855.jpg 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-1320x990.jpg 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/06\/How-to-Install-and-Configure-Nginx-on-an-Ubuntu-18.04-1440x1080.jpg 1440w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/768;\" \/><\/p>\n<p>Nginx is a high-performance <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-are-web-servers-and-why-are-they-needed\/\">web server<\/a> and <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-reverse-proxy\/\">reverse proxy<\/a>. It is also used as a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-load-balancing\/\">load balancer<\/a>, HTTP cache, and mail proxy. Compared to <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/nginx-server-explained\/\">Nginx<\/a> is known for its high performance and low memory usage which makes it ideal for high traffic websites.<\/p>\n<p>This tutorial will guide you through the process of installing and configuring Nginx on an <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-ubuntu-beginners-guide-ubuntu-linux-distro\/\">Ubuntu<\/a> 18.04 server. By the end of this guide, you will have a running Nginx server that you can use to host your websites.<\/p>\n<p>Before we start, it&#8217;s important to note that you should have a non-root user account with sudo privileges set up on your server. Also, ensure that your server is updated with the latest security patches and updates.<\/p>\n<h2>Step 1: Update Your System<\/h2>\n<p>The first step in installing any package on your Ubuntu system is to ensure that the system&#8217;s package manager, apt, is up to date. Run the following command to update the package list:<\/p>\n<pre>\r\nsudo apt update\r\n<\/pre>\n<p>This command will fetch the latest version of all packages currently installed on your system from the repositories.<\/p>\n<h2>Step 2: Install Nginx<\/h2>\n<p>With your system up to date, you can proceed to install Nginx. Nginx is included in the Ubuntu 18.04 default repositories, so the installation is straightforward. Run the following command:<\/p>\n<pre>\r\nsudo apt install nginx\r\n<\/pre>\n<p>During the installation, Ubuntu will ask you for your password. Enter it to proceed with the installation.<\/p>\n<h2>Step 3: Adjust the Firewall<\/h2>\n<p>Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw, Ubuntu&#8217;s firewall, upon installation. This makes it rather easy to allow Nginx access.<\/p>\n<p>You can list the applications configurations that ufw knows how to work with by typing:<\/p>\n<pre>\r\nsudo ufw app list\r\n<\/pre>\n<p>You should see a list of all the application profiles, including Nginx. To allow incoming HTTP traffic, you can use the following command:<\/p>\n<pre>\r\nsudo ufw allow 'Nginx HTTP'\r\n<\/pre>\n<p>You can verify the change by typing:<\/p>\n<pre>\r\nsudo ufw status\r\n<\/pre>\n<p>You should see HTTP traffic allowed in the displayed output.<\/p>\n<h2>Step 4: Check your Web Server<\/h2>\n<p>At the end of the installation process, Ubuntu 18.04 starts Nginx. The web server should already be up and running.<\/p>\n<p>You can check with the systemd init system to make sure the service is running by typing:<\/p>\n<pre>\r\nsystemctl status nginx\r\n<\/pre>\n<p>If everything was done correctly, you should see an active (running) status.<\/p>\n<h2>Step 5: Manage the Nginx Process<\/h2>\n<p>Now that you have your web server up and running, let&#8217;s go over some basic management commands.<\/p>\n<p>To stop your web server, you can type:<\/p>\n<pre>\r\nsudo systemctl stop nginx\r\n<\/pre>\n<p>To start the web server when it is stopped, type:<\/p>\n<pre>\r\nsudo systemctl start nginx\r\n<\/pre>\n<p>To stop and then start the service again, type:<\/p>\n<pre>\r\nsudo systemctl restart nginx\r\n<\/pre>\n<p>If you are simply making configuration changes, Nginx can often reload without dropping connections. To do this, type:<\/p>\n<pre>\r\nsudo systemctl reload nginx\r\n<\/pre>\n<p>By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:<\/p>\n<pre>\r\nsudo systemctl disable nginx\r\n<\/pre>\n<p>To re-enable the service to start up at boot, youcan type:<\/p>\n<pre>\r\nsudo systemctl enable nginx\r\n<\/pre>\n<h2>Step 6: Set Up Server Blocks (Virtual Hosts)<\/h2>\n<p>If you plan on hosting multiple domains on your server, you can set up Nginx server blocks, similar to Apache&#8217;s virtual hosts. This allows you to host more than one website on a single machine.<\/p>\n<p>First, create the directory for your domain:<\/p>\n<pre>\r\nsudo mkdir -p \/var\/www\/your_domain\/html\r\n<\/pre>\n<p>Next, assign ownership of the directory with the $USER environment variable:<\/p>\n<pre>\r\nsudo chown -R $USER:$USER \/var\/www\/your_domain\/html\r\n<\/pre>\n<p>The permissions of your web roots should be correct if you haven&#8217;t modified your umask value, but you can make sure by typing:<\/p>\n<pre>\r\nsudo chmod -R 755 \/var\/www\/your_domain\r\n<\/pre>\n<p>Next, create a sample index.html page using nano or your favorite text editor:<\/p>\n<pre>\r\nnano \/var\/www\/your_domain\/html\/index.html\r\n<\/pre>\n<p>Inside, add the following sample HTML:<\/p>\n<pre>\r\n&lt;html&gt;\r\n &lt;head&gt;\r\n &lt;title&gt;Welcome to Your_domain!&lt;\/title&gt;\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n &lt;h1&gt;Success! The your_domain server block is working!&lt;\/h1&gt;\r\n &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Save and close the file when you are finished.<\/p>\n<h2>Step 7: Create Server Block Files for Each Domain<\/h2>\n<p>Next, you will need to create a new server block configuration file for each domain you are hosting. Start by creating a new file in the sites-available directory:<\/p>\n<pre>\r\nsudo nano \/etc\/nginx\/sites-available\/your_domain\r\n<\/pre>\n<p>Inside, paste the following configuration:<\/p>\n<pre>\r\nserver {\r\n        listen 80;\r\n        listen [::]:80;\r\n\r\n        root \/var\/www\/your_domain\/html;\r\n        index index.html index.htm index.nginx-debian.html;\r\n\r\n        server_name your_domain www.your_domain;\r\n\r\n        location \/ {\r\n                try_files $uri $uri\/ =404;\r\n        }\r\n}\r\n<\/pre>\n<p>Save and close the file when you are finished. Then, enable the file by creating a link from it to the sites-enabled directory:<\/p>\n<pre>\r\nsudo ln -s \/etc\/nginx\/sites-available\/your_domain \/etc\/nginx\/sites-enabled\/\r\n<\/pre>\n<p>Finally, test for syntax errors:<\/p>\n<pre>\r\nsudo nginx -t\r\n<\/pre>\n<p>If no problems were found, restart Nginx to enable your changes:<\/p>\n<pre>\r\nsudo systemctl restart nginx\r\n<\/pre>\n<h2>Step 8: Test your Results<\/h2>\n<p>Now that you have your server blocks set up, you can test your setup by navigating to the domains in your web browser:<\/p>\n<pre>\r\nhttp:\/\/your_domain\r\n<\/pre>\n<p>You should see your web page displaying the text &#8220;Success! The your_domain server block is working!&#8221;. If you see this page, then your server block is working correctly.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we have walked through the process of installing and configuring Nginx on an Ubuntu 18.04 server. We have covered updating your system, installing Nginx, adjusting the firewall, checking your web server, managing the Nginx process, setting up server blocks (virtual hosts), creating server block files for each domain, and testing your results.<\/p>\n<p>By following these steps, you should now have a fully functioning Nginx server on your Ubuntu 18.04 system. This setup is a good starting point for hosting your websites and exploring more advanced Nginx configurations. Remember, Nginx is a powerful and flexible web server capable of handling many simultaneous connections, and as such, it is ideal for serving static content, acting as a reverse proxy, and for load balancing.<\/p>\n<p>If you&#8217;re interested in exploring other web server options, our guide on the <a href=\"https:\/\/webhostinggeeks.com\/best\/web-server\/\">best web servers<\/a> can provide you with a comparison of the most popular servers available today.<\/p>\n<p>Remember, the choice of web server largely depends on the requirements of your website and your technical expertise. Whether you&#8217;re looking for a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server<\/a>, a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS server<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud hosting<\/a>, or <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-shared-hosting\/\">shared hosting<\/a>, we&#8217;ve got you covered.<\/p>\n<p>Happy hosting!<\/p>\n<h2>FAQs<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is Nginx?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Nginx is a high-performance HTTP server and reverse proxy. It is also used as a load balancer, HTTP cache, and mail proxy. Compared to Apache, Nginx is known for its high performance and low memory usage.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Why use Nginx over Apache?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Nginx is known for its high performance and low memory usage, which makes it ideal for high traffic websites. It also has a more straightforward configuration than Apache.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is a server block in Nginx?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">A server block in Nginx is equivalent to a virtual host in Apache. It allows you to host more than one website on a single machine.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I start and stop Nginx?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can start Nginx with the command &#8216;sudo systemctl start nginx&#8217; and stop it with &#8216;sudo systemctl stop nginx&#8217;. To restart Nginx, use &#8216;sudo systemctl restart nginx&#8217;.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I test my Nginx configuration?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">You can test your Nginx configuration with the command &#8216;sudo nginx -t&#8217;. This will check for syntax errors and report any issues.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">sudo apt update<\/span> \u2013 Updates the package list for the system.<\/li>\n<li><span class=\"fw-bold\">sudo apt install nginx<\/span> \u2013 Installs Nginx on the system.<\/li>\n<li><span class=\"fw-bold\">sudo ufw app list<\/span> \u2013 Lists the application profiles known to ufw.<\/li>\n<li><span class=\"fw-bold\">sudo ufw allow &#8216;Nginx HTTP&#8217;<\/span> \u2013 Allows incoming HTTP traffic on Nginx.<\/li>\n<li><span class=\"fw-bold\">sudo ufw status<\/span> \u2013 Checks the status of ufw.<\/li>\n<li><span class=\"fw-bold\">systemctl status nginx<\/span> \u2013 Checks the status of the Nginx service.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl stop nginx<\/span> \u2013 Stops the Nginx service.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl start nginx<\/span> \u2013 Starts the Nginx service.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl restart nginx<\/span> \u2013 Restarts the Nginx service.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl reload nginx<\/span> \u2013 Reloads the Nginx service without dropping connections.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl disable nginx<\/span> \u2013 Disables the Nginx service from starting at boot.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl enable nginx<\/span> \u2013 Enables the Nginx service to start at boot.<\/li>\n<li><span class=\"fw-bold\">sudo mkdir -p \/var\/www\/your_domain\/html<\/span> \u2013 Creates the directory for your domain.<\/li>\n<li><span class=\"fw-bold\">sudo chown -R $USER:$USER \/var\/www\/your_domain\/html<\/span> \u2013 Assigns ownership of the directory.<\/li>\n<li><span class=\"fw-bold\">sudo chmod -R 755 \/var\/www\/your_domain<\/span> \u2013 Sets the correct permissions for your web roots.<\/li>\n<li><span class=\"fw-bold\">sudo nano \/etc\/nginx\/sites-available\/your_domain<\/span> \u2013 Opens a new server block configuration file for editing.<\/li>\n<li><span class=\"fw-bold\">sudo ln -s \/etc\/nginx\/sites-available\/your_domain \/etc\/nginx\/sites-enabled\/<\/span> \u2013 Creates a link from the sites-available directory to the sites-enabled directory.<\/li>\n<li><span class=\"fw-bold\">sudo nginx -t<\/span> \u2013 Tests for syntax errors in the Nginx configuration files.<\/li>\n<li><span class=\"fw-bold\">sudo systemctl restart nginx<\/span> \u2013 Restarts the Nginx service to enable changes.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Nginx is a high-performance web server and reverse proxy. It is also used as a load balancer, HTTP cache, and mail proxy. Compared to Apache, Nginx is known for its&#8230;<\/p>\n","protected":false},"author":6,"featured_media":16018,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1014,1073],"tags":[1605,1856,2074],"class_list":["post-16017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx","category-ubuntu","tag-nginx","tag-ubuntu","tag-ubuntu-18-04"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/16017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=16017"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/16017\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/16018"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=16017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=16017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=16017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}