{"id":49,"date":"2023-04-30T04:03:54","date_gmt":"2023-04-30T04:03:54","guid":{"rendered":"https:\/\/minifyhtmlcssjs.com\/blog\/?p=49"},"modified":"2023-04-30T04:09:46","modified_gmt":"2023-04-30T04:09:46","slug":"how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/minifyhtmlcssjs.com\/blog\/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-ubuntu-20-04\/","title":{"rendered":"How to Configure Nginx as a Web Server and Reverse Proxy for Apache on Ubuntu 20.04"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-52 alignleft\" src=\"https:\/\/minifyhtmlcssjs.com\/blog\/wp-content\/uploads\/2023\/04\/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-ubuntu-20-04-300x174.png\" alt=\"How to Configure Nginx as a Web Server and Reverse Proxy for Apache on Ubuntu 20.04\" width=\"300\" height=\"174\" srcset=\"https:\/\/minifyhtmlcssjs.com\/blog\/wp-content\/uploads\/2023\/04\/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-ubuntu-20-04-300x174.png 300w, https:\/\/minifyhtmlcssjs.com\/blog\/wp-content\/uploads\/2023\/04\/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-ubuntu-20-04-768x446.png 768w, https:\/\/minifyhtmlcssjs.com\/blog\/wp-content\/uploads\/2023\/04\/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-ubuntu-20-04.png 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>Nginx is a highly popular and efficient web server that is widely used on Linux servers. It allows you to configure it as a standalone web server or as a reverse proxy for other web applications. In this article, we will learn how to configure Nginx as a web server and reverse proxy for Apache on a Ubuntu 20.04 server in your <a href=\"https:\/\/www.webmastersun.com\/forums\/95-web-hosting\/\">web hosting<\/a>.<\/p>\n<h2>Step 1: Install Nginx and Apache on Ubuntu 20.04<\/h2>\n<p>First, we need to install Nginx and Apache on our Ubuntu 20.04 server. You can do this by using the following commands in the terminal:<\/p>\n<blockquote><p>sudo apt update<br \/>\nsudo apt install nginx apache2<\/p><\/blockquote>\n<p><!--more--><\/p>\n<h2>Step 2: Configure Nginx as a Web Server<\/h2>\n<p>After installing Nginx, we will configure it as a standalone web server. To do this, we need to create a new configuration file for Nginx. Use your favorite text editor to create a file named <code>example.com<\/code> in the directory <code>\/etc\/nginx\/sites-available\/<\/code> with the following content:<\/p>\n<blockquote><p>server {<br \/>\nlisten 80;<br \/>\nlisten [::]:80;<br \/>\nserver_name example.com www.example.com;<\/p>\n<p>root \/var\/www\/example.com\/html;<br \/>\nindex index.html;<\/p>\n<p>location \/ {<br \/>\ntry_files $uri $uri\/ =404;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>This configuration file tells Nginx to listen on port 80 for requests to the domain <code>example.com<\/code> and its <code>www<\/code> subdomain. It also specifies the root directory for the website files and the default index file.<\/p>\n<p>Next, we need to create a symbolic link to enable this configuration file. Use the following command to create the link:<\/p>\n<blockquote><p>sudo ln -s \/etc\/nginx\/sites-available\/example.com \/etc\/nginx\/sites-enabled\/<\/p><\/blockquote>\n<p>Finally, restart Nginx to apply the new configuration:<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<blockquote>\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">sudo systemctl restart nginx<\/div>\n<\/blockquote>\n<div>\n<p>Now, you can access your website by visiting <code>http:\/\/example.com<\/code> or <code>http:\/\/www.example.com<\/code>.<\/p>\n<h2>Step 3: Configure Nginx as a Reverse Proxy for Apache<\/h2>\n<p>Next, we will configure Nginx as a reverse proxy for Apache. This will allow Nginx to handle incoming requests and forward them to Apache, which will serve the actual web pages.<\/p>\n<p>First, we need to stop Apache from listening on the default port 80. Use the following command to disable the default Apache site:<\/p>\n<\/div>\n<blockquote><p>sudo a2dissite 000-default<\/p><\/blockquote>\n<p>Next, we need to create a new Apache configuration file for our website. Use your favorite text editor to create a file named <code>example.com.conf<\/code> in the directory <code>\/etc\/apache2\/sites-available\/<\/code> with the following content:<\/p>\n<blockquote><p>&lt;VirtualHost *:8080&gt;<br \/>\nServerAdmin webmaster@example.com<br \/>\nServerName example.com<br \/>\nDocumentRoot \/var\/www\/example.com\/html<\/p>\n<p>&lt;Directory \/var\/www\/example.com\/html&gt;<br \/>\nOptions Indexes FollowSymLinks MultiViews<br \/>\nAllowOverride All<br \/>\nOrder allow,deny<br \/>\nallow from all<br \/>\n&lt;\/Directory&gt;<br \/>\n&lt;\/VirtualHost&gt;<\/p><\/blockquote>\n<p>This configuration file tells Apache to listen on port 8080 for requests to the domain <code>example.com<\/code>. It also specifies the root directory for the website files and enables the use of <code>.htaccess<\/code> files.<\/p>\n<p>Next, we need to enable this Apache site and restart Apache:<\/p>\n<blockquote><p>sudo a2ensite example.com.conf<br \/>\nsudo systemctl restart apache2<\/p><\/blockquote>\n<p>Finally, we need to configure Nginx as a reverse proxy for Apache. Use your favorite text editor to modify the <code>\/etc\/nginx\/sites-available\/example.com<\/code> configuration file as follows:<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<blockquote>\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\">server {<br \/>\nlisten 80;<br \/>\nlisten [::]:80;<br \/>\nserver_name example.com www.example.com<\/div>\n<\/blockquote>\n<div>After modifying the server_name directive, we need to add the reverse proxy configuration to the Nginx configuration file. Here&#8217;s how:<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div>\n<blockquote><p>server {<br \/>\nlisten 80;<br \/>\nlisten [::]:80;<br \/>\nserver_name example.com www.example.com;<\/p>\n<p>location \/ {<br \/>\nproxy_pass http:\/\/localhost:8080;<br \/>\nproxy_set_header Host $host;<br \/>\nproxy_set_header X-Real-IP $remote_addr;<br \/>\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br \/>\n}<\/p>\n<p>location \/static\/ {<br \/>\nalias \/var\/www\/example.com\/static\/;<br \/>\nexpires 1h;<br \/>\nadd_header Cache-Control &#8220;public, max-age=3600&#8221;;<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<\/div>\n<div>\n<p>The <code>location \/<\/code> block tells Nginx to pass all requests to <code>http:\/\/localhost:8080<\/code>, which is where Apache is listening. The proxy_set_header directives pass along some additional information about the request to Apache, such as the original host name and IP address.<\/p>\n<p>The <code>location \/static\/<\/code> block is used to serve static files directly from Nginx, without passing them through Apache. This can improve performance for static files like images, CSS, and JavaScript files.<\/p>\n<p>Finally, restart Nginx to apply the new configuration:<\/p>\n<blockquote><p>sudo systemctl restart nginx<\/p><\/blockquote>\n<div class=\"group w-full text-gray-800 dark:text-gray-100 border-b border-black\/10 dark:border-gray-900\/50 bg-gray-50 dark:bg-[#444654]\">\n<div class=\"text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto\">\n<div class=\"relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]\">\n<div class=\"flex flex-grow flex-col gap-3\">\n<div class=\"min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap break-words\">\n<div class=\"markdown prose w-full break-words dark:prose-invert dark\">\n<p>Now, Nginx will handle all incoming requests for <code>http:\/\/example.com<\/code> and <code>http:\/\/www.example.com<\/code>. It will pass requests for dynamic content to Apache and serve static files directly.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"flex justify-between lg:block\">\n<div class=\"text-gray-400 flex self-end lg:self-center justify-center mt-2 gap-2 md:gap-3 lg:gap-1 lg:absolute lg:top-0 lg:translate-x-full lg:right-0 lg:mt-0 lg:pl-2 visible\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"group w-full text-gray-800 dark:text-gray-100 border-b border-black\/10 dark:border-gray-900\/50 dark:bg-gray-800\">\n<div class=\"text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto\">\n<div class=\"w-[30px] flex flex-col relative items-end\">\n<div class=\"relative flex\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Nginx is a highly popular and efficient web server that is widely used on Linux servers. It allows you to configure it as a standalone web server or as a reverse proxy for other web applications. In this article, we will learn how to configure Nginx as a web server and reverse proxy for Apache [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[36,35,34],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-web-hosting","tag-nginx","tag-server","tag-web-hosting"],"_links":{"self":[{"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":5,"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":55,"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions\/55"}],"wp:attachment":[{"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/minifyhtmlcssjs.com\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}