{"id":1948,"date":"2016-06-16T13:05:03","date_gmt":"2016-06-16T17:05:03","guid":{"rendered":"https:\/\/www.globo.tech\/learning-center\/?p=1948"},"modified":"2017-11-24T17:57:53","modified_gmt":"2017-11-24T22:57:53","slug":"setup-nginx-reverse-proxy-apache-ubuntu-16","status":"publish","type":"post","link":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/","title":{"rendered":"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16"},"content":{"rendered":"<h1>How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16<\/h1>\n<p>Nginx and Apache are the most popular open source web servers, and each has distinct characteristics. Nginx is often lighter and faster, but this comes at a cost since it does not automatically support PHP and other common web technologies. Apache, on the other hand, uses more resources and is more complicated to configure. In contrast to Nginx, it can run PHP and other scripting languages via its integrated module system.<\/p>\n<p>Most webmasters run PHP in a separate FPM process behind Nginx, but there are advantages to proxying PHP requests to Apache. First, you can access the .htaccess files shipped with most PHP apps, without building custom  rules. You can also use an app&#8217;s Apache configuration directly without having to map directives to Nginx. Nginx can continue serving static files, something it does amazingly well. You can also add other modules to Apache later, letting it handle scripts while Nginx serves everything else.<\/p>\n<h2>Getting Started<\/h2>\n<p>To complete this guide, you will need the following:<br \/>\n\u2022 1 Node (<a href=\"https:\/\/www.globo.tech\/cloud-server-pricing\" target=\"_blank\">Cloud Server<\/a> or <a href=\"http:\/\/www.globo.tech\/dedicated-server-hosting\" target=\"_blank\">Dedicated Server<\/a>) with a plain Ubuntu 16.04.<\/p>\n<p>When we&#8217;re done, you&#8217;ll have an Nginx web server proxying PHP requests to Apache. While out of scope for this tutorial, much of it can be used to run other scripting languages in Apache as well.<\/p>\n<h2>Tutorial<\/h2>\n<p>Let&#8217;s go. First we&#8217;ll update our Apt cache and installed packages. This not only patches known current security vulnerabilities, but it also ensures that we won&#8217;t receive errors about missing packages later.<\/p>\n<p><code> apt-get update &amp;&amp; apt-get upgrade -y &amp;&amp; shutdown -r now<\/code><\/p>\n<p>Next let&#8217;s install the standard apache2 package.<\/p>\n<p><code> apt-get install apache2 -y<br \/>\n apt-get install php libapache2-mod-php php-mcrypt -y<\/code><\/p>\n<p>We must now tweak Apache&#8217;s port, since it will now be a proxy behind Nginx and can&#8217;t conflict.<\/p>\n<p><code> nano \/etc\/apache2\/ports.conf<\/code><\/p>\n<p>Change the line:<\/p>\n<p><code class=\"gris\">Listen 80<\/code><\/p>\n<p>to:<\/p>\n<p><code class=\"gris\">Listen 8080<\/code><\/p>\n<p>Save this file and exit.<\/p>\n<p>Since Apache will be handling our PHP scripts, we need to enable its PHP module.<\/p>\n<p><code> nano \/etc\/apache2\/mods-enabled\/dir.conf<\/code><\/p>\n<p>Find this block:<\/p>\n<p><code class=\"gris\">&lt;IfModule mod_dir.c&gt;<br \/>\nDirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm<br \/>\n&lt;\/IfModule&gt;<\/code><\/p>\n<p>Place index.php at the front.<\/p>\n<p><code class=\"gris\">&lt;IfModule mod_dir.c&gt;<br \/>\nDirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm<br \/>\n&lt;\/IfModule&gt;<\/code><\/p>\n<p>Save and exit.<\/p>\n<p>To test our setup, we&#8217;ll create a phpinfo page. This will display lots of diagnostics about your current installation.<\/p>\n<p><code>&lt;?php<br \/>\nphpinfo();<br \/>\n?&gt;<\/code><\/p>\n<p>Enter the document root directory and create a page called index.php.<\/p>\n<p><code> cd \/var\/www\/html<br \/>\n nano index.php<\/code><\/p>\n<p>Save and exit. Visit http:\/\/your_main_ip:8080\/ to see if this page works.<\/p>\n<p>Now we need to install Nginx, which will be proxying all of your PHP web traffic. Static resources will be served directly by Nginx.<\/p>\n<p><code> apt-get install nginx -y<\/code><\/p>\n<p>To save bandwidth and increase transfer speed, we&#8217;ll now enable Gzip compression on the server. Gzip will compress text and other resources to speed up downloads.<\/p>\n<p><code> nano \/etc\/nginx\/nginx.conf<br \/>\ngzip on;<br \/>\ngzip_disable \"msie6\";<br \/>\ngzip_vary on;<br \/>\ngzip_proxied any;<br \/>\ngzip_comp_level 6;<br \/>\ngzip_buffers 16 8k;<br \/>\ngzip_http_version 1.1;<br \/>\ngzip_types text\/plain text\/css application\/json application\/javascript text\/xml application\/xml application\/xml+rss text\/javascript;<\/code><\/p>\n<p>Under the gzip_types line, add these two:<\/p>\n<p><code># Proxy Cache Settings<br \/>\nproxy_cache_path \/var\/cache levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;<\/code><\/p>\n<p>Save and exit. We&#8217;ll next need to modify the Nginx virtual host so it proxies PHP requests to Apache.<\/p>\n<p><code> nano \/etc\/nginx\/sites-enabled\/default<\/code><\/p>\n<p>Find this block of text:<\/p>\n<p><code class=\"gris\"># Add index.php to the list if you are using PHP<br \/>\nindex index.html index.htm index.nginx-debian.html;<\/code><\/p>\n<p>Add index.php as shown:<\/p>\n<p><code># Add index.php to the list if you are using PHP<br \/>\nindex index.php index.html index.htm index.nginx-debian.html;<\/code><\/p>\n<p>Next, after this block:<\/p>\n<p><code class=\"gris\">server_name _;<br \/>\nlocation \/ {<br \/>\n# First attempt to serve request as file, then<br \/>\n# as directory, then fall back to displaying a 404.<br \/>\ntry_files $uri $uri\/ =404;<br \/>\n}<\/code><\/p>\n<p>add:<\/p>\n<p><code class=\"gris\"># Reverse Proxy and Proxy Cache Configuration<br \/>\nlocation ~ \\.php$ {proxy_set_header X-Real-IP $remote_addr;<br \/>\nproxy_set_header X-Forwarded-For $remote_addr;<br \/>\nproxy_set_header Host $host;<br \/>\nproxy_pass http:\/\/127.0.0.1:8080;# Cache configuration<br \/>\nproxy_cache reverse_cache;<br \/>\nproxy_cache_valid 3s;<br \/>\nproxy_no_cache $cookie_PHPSESSID;<br \/>\nproxy_cache_bypass $cookie_PHPSESSID;<br \/>\nproxy_cache_key \"$scheme$host$request_uri\";<br \/>\nadd_header X-Cache $upstream_cache_status;<br \/>\n}<br \/>\n# Enable Cache the file 30 days<br \/>\nlocation ~* .(jpg|png|gif|jpeg|css|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {<br \/>\nproxy_cache_valid 200 120m;<br \/>\nexpires 30d;<br \/>\nproxy_cache reverse_cache;<br \/>\naccess_log off;<br \/>\n}<br \/>\n# Disable Cache for the file type html, json<br \/>\nlocation ~* .(?:manifest|appcache|html?|xml|json)$ {<br \/>\nexpires -1;<br \/>\n}<br \/>\nlocation ~ \/\\.ht {<br \/>\ndeny all;<br \/>\n}<\/code><\/p>\n<p>Save and exit. You will need to restart the service, to apply changes. Now the same phpinfo page is available at http:\/\/your_main_ip.<\/p>\n<h2>Conclusion<\/h2>\n<p>You&#8217;re now routing PHP traffic through Nginx to Apache. This setup gives the best of both worlds a light, efficient server for static files, and a robust PHP environment for running PHP scripts. If this guide was helpful to you, kindly share it with others who may also be interested. <\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 Nginx and Apache are the most popular open source web servers, and each has distinct characteristics. Nginx is often lighter and faster, but this comes at a cost since it does not automatically support PHP and other common web technologies. Apache, on the other<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-1948","post","type-post","status-publish","format-standard","hentry","category-web-hosting","operating_system-ubuntu-16-04"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 - Globo.Tech<\/title>\n<meta name=\"description\" content=\"This tutorial will walk you through the setup of NGINX reverse proxy over Apache on your Ubuntu 16.04 LTS server. Read now!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 - Globo.Tech\" \/>\n<meta property=\"og:description\" content=\"This tutorial will walk you through the setup of NGINX reverse proxy over Apache on your Ubuntu 16.04 LTS server. Read now!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/\" \/>\n<meta property=\"og:site_name\" content=\"Globo.Tech\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-16T17:05:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-24T22:57:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"963\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"GloboTech Communications\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"GloboTech Communications\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/\"},\"author\":{\"name\":\"GloboTech Communications\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#\\\/schema\\\/person\\\/e17784b37f4a4f49b7bc611847912e87\"},\"headline\":\"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16\",\"datePublished\":\"2016-06-16T17:05:03+00:00\",\"dateModified\":\"2017-11-24T22:57:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/\"},\"wordCount\":550,\"commentCount\":2,\"articleSection\":[\"Web Hosting\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/\",\"url\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/\",\"name\":\"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 - Globo.Tech\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#website\"},\"datePublished\":\"2016-06-16T17:05:03+00:00\",\"dateModified\":\"2017-11-24T22:57:53+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#\\\/schema\\\/person\\\/e17784b37f4a4f49b7bc611847912e87\"},\"description\":\"This tutorial will walk you through the setup of NGINX reverse proxy over Apache on your Ubuntu 16.04 LTS server. Read now!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/setup-nginx-reverse-proxy-apache-ubuntu-16\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#website\",\"url\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/\",\"name\":\"Globo.Tech\",\"description\":\"Welcome to the Official Globo.Tech Learning Center\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.globo.tech\\\/learning-center\\\/#\\\/schema\\\/person\\\/e17784b37f4a4f49b7bc611847912e87\",\"name\":\"GloboTech Communications\",\"sameAs\":[\"http:\\\/\\\/www.gtcomm.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 - Globo.Tech","description":"This tutorial will walk you through the setup of NGINX reverse proxy over Apache on your Ubuntu 16.04 LTS server. Read now!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/","og_locale":"en_US","og_type":"article","og_title":"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 - Globo.Tech","og_description":"This tutorial will walk you through the setup of NGINX reverse proxy over Apache on your Ubuntu 16.04 LTS server. Read now!","og_url":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/","og_site_name":"Globo.Tech","article_published_time":"2016-06-16T17:05:03+00:00","article_modified_time":"2017-11-24T22:57:53+00:00","og_image":[{"width":1920,"height":963,"url":"https:\/\/www.globo.tech\/learning-center\/wp-content\/uploads\/2016\/09\/GloboTech-Logo.png","type":"image\/png"}],"author":"GloboTech Communications","twitter_misc":{"Written by":"GloboTech Communications","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/#article","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/"},"author":{"name":"GloboTech Communications","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"headline":"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16","datePublished":"2016-06-16T17:05:03+00:00","dateModified":"2017-11-24T22:57:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/"},"wordCount":550,"commentCount":2,"articleSection":["Web Hosting"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/","url":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/","name":"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16 - Globo.Tech","isPartOf":{"@id":"https:\/\/www.globo.tech\/learning-center\/#website"},"datePublished":"2016-06-16T17:05:03+00:00","dateModified":"2017-11-24T22:57:53+00:00","author":{"@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87"},"description":"This tutorial will walk you through the setup of NGINX reverse proxy over Apache on your Ubuntu 16.04 LTS server. Read now!","breadcrumb":{"@id":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.globo.tech\/learning-center\/setup-nginx-reverse-proxy-apache-ubuntu-16\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.globo.tech\/learning-center\/"},{"@type":"ListItem","position":2,"name":"How to Setup NGINX Reverse Proxy Over Apache on Ubuntu 16"}]},{"@type":"WebSite","@id":"https:\/\/www.globo.tech\/learning-center\/#website","url":"https:\/\/www.globo.tech\/learning-center\/","name":"Globo.Tech","description":"Welcome to the Official Globo.Tech Learning Center","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.globo.tech\/learning-center\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.globo.tech\/learning-center\/#\/schema\/person\/e17784b37f4a4f49b7bc611847912e87","name":"GloboTech Communications","sameAs":["http:\/\/www.gtcomm.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/1948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/comments?post=1948"}],"version-history":[{"count":13,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/1948\/revisions"}],"predecessor-version":[{"id":3893,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/posts\/1948\/revisions\/3893"}],"wp:attachment":[{"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/media?parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/categories?post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.globo.tech\/learning-center\/wp-json\/wp\/v2\/tags?post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}