{"id":5131,"date":"2014-06-10T00:33:08","date_gmt":"2014-06-09T16:33:08","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=5131"},"modified":"2023-04-28T09:48:18","modified_gmt":"2023-04-28T09:48:18","slug":"how-to-install-nginx-with-php5-and-php-fpm-and-mysql-lemp-on-ubuntu-14-04","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-install-nginx-with-php5-and-php-fpm-and-mysql-lemp-on-ubuntu-14-04\/","title":{"rendered":"How to Install Nginx With PHP5 (and PHP-FPM) and MySQL (LEMP) On Ubuntu 14.04"},"content":{"rendered":"<p>NGINX, pronounced \u201cEngine X\u201d is an alternate web server for Apache. NGINX is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. If you plan to run a very busy and high concurrency websites but with low memory usage, i would suggest you to setup NGINX as a web server on your Virtual Private Server (VPS) or dedicated server.<\/p>\n<p>1. Install nginx, PHP and MySQL :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo apt-get install nginx php5 php5-fpm php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl mysql-server-5.6 -y\n<\/pre>\n<p>During this installation, you will require to set MySQL&#8217;s root password :<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/05\/1.png\" alt=\"1\" width=\"564\" height=\"291\" class=\"alignnone size-full wp-image-5108 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/05\/1.png 564w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/05\/1-300x155.png 300w\" data-sizes=\"(max-width: 564px) 100vw, 564px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 564px; --smush-placeholder-aspect-ratio: 564\/291;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/05\/2.png\" alt=\"2\" width=\"483\" height=\"249\" class=\"alignnone size-full wp-image-5109 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/05\/2.png 483w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/05\/2-300x155.png 300w\" data-sizes=\"(max-width: 483px) 100vw, 483px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 483px; --smush-placeholder-aspect-ratio: 483\/249;\" \/><\/p>\n<p>2. Open \/etc\/php5\/fpm\/php.ini and set cgi.fix_pathinfo=0:<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo vi \/etc\/php5\/fpm\/php.ini\n<\/pre>\n<pre>\n; http:\/\/php.net\/cgi.fix-pathinfo\ncgi.fix_pathinfo=0\n\n; FastCGI under IIS (on WINNT based OS) supports the ability to impersonate\n; securi\n<\/pre>\n<p>3. Set date.timezone in \/etc\/php5\/fpm\/php.ini :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo vi \/etc\/php5\/fpm\/php.ini\n<\/pre>\n<pre>\n[Date]\n; Defines the default timezone used by the date functions\n; http:\/\/php.net\/date.timezone\ndate.timezone = \"Asia\/Kuala_Lumpur\"\n\n; http:\/\/php.net\/date.default-latitude\n;date.default_latitude = 31.7667\n<\/pre>\n<p>4. By default PHP-FPM is listening on the socket \/var\/run\/php5-fpm.sock. We have an option to make PHP-FPM use a TCP connection. Open and add &#8220;listen = 127.0.0.1:9000&#8221; and comment out &#8220;listen = \/var\/run\/php5-fpm.sock&#8221;<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo vim \/etc\/php5\/fpm\/pool.d\/www.conf\n<\/pre>\n<p>Change as below :<\/p>\n<pre>\n..\n..\n;   '\/path\/to\/unix\/socket' - to listen on a unix socket.\n; Note: This value is mandatory.\n;listen = \/var\/run\/php5-fpm.sock\nlisten = 127.0.0.1:9000\n..\n..\n<\/pre>\n<p>5. Restart PHP-FPM :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo service php5-fpm restart\nstop: Unknown instance:\nphp5-fpm start\/running, process 7314\n<\/pre>\n<p>6. Backup NGINX config file :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo cp -p \/etc\/nginx\/nginx.conf \/etc\/nginx\/nginx.conf.bak\n<\/pre>\n<p>7. Check Number CPU on your VPS server. This will will require to adjust NGINX Worker Processes &#038; Connections. NGINX workers is equal to the number of processors :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ lscpu | grep '^CPU(s)'\nCPU(s):                1\n<\/pre>\n<p>8. Configure worker_processes on nginx.conf as below :<\/p>\n<pre>\n..\n..\nworker_processes 1;\n..\n..\n<\/pre>\n<p>Full nginx configuration should like below :<\/p>\n<pre>\nuser www-data;\nworker_processes 1;\npid \/run\/nginx.pid;\n\nevents {\n        worker_connections 768;\n        # multi_accept on;\n}\n\nhttp {\n\n        ##\n        # Basic Settings\n        ##\n\n        sendfile on;\n        tcp_nopush on;\n        tcp_nodelay on;\n        keepalive_timeout 65;\n        types_hash_max_size 2048;\n        # server_tokens off;\n\n        # server_names_hash_bucket_size 64;\n        # server_name_in_redirect off;\n\n        include \/etc\/nginx\/mime.types;\n        default_type application\/octet-stream;\n\n        ##\n        # Logging Settings\n        ##\n\n        access_log \/var\/log\/nginx\/access.log;\n        error_log \/var\/log\/nginx\/error.log;\n\n        ##\n        # Gzip Settings\n        ##\n\n        gzip on;\n        gzip_disable \"msie6\";\n\n        # gzip_vary on;\n        # gzip_proxied any;\n        # gzip_comp_level 6;\n        # gzip_buffers 16 8k;\n        # gzip_http_version 1.1;\n        # gzip_types text\/plain text\/css application\/json application\/x-javascript text\/xml application\/xml application\/xml+rss text\/javascript;\n\n        ##\n        # nginx-naxsi config\n        ##\n        # Uncomment it if you installed nginx-naxsi\n        ##\n\n        #include \/etc\/nginx\/naxsi_core.rules;\n\n        ##\n        # nginx-passenger config\n        ##\n        # Uncomment it if you installed nginx-passenger\n        ##\n\n        #passenger_root \/usr;\n        #passenger_ruby \/usr\/bin\/ruby;\n\n        ##\n        # Virtual Host Configs\n        ##\n\n        include \/etc\/nginx\/conf.d\/*.conf;\n        include \/etc\/nginx\/sites-enabled\/*;\n}\n\n\n#mail {\n#       # See sample authentication script at:\n#       # http:\/\/wiki.nginx.org\/ImapAuthenticateWithApachePhpScript\n#\n#       # auth_http localhost\/auth.php;\n#       # pop3_capabilities \"TOP\" \"USER\";\n#       # imap_capabilities \"IMAP4rev1\" \"UIDPLUS\";\n#\n#       server {\n#               listen     localhost:110;\n#               protocol   pop3;\n#               proxy      on;\n#       }\n#\n#       server {\n#               listen     localhost:143;\n#               protocol   imap;\n#               proxy      on;\n#       }\n#}\n\n<\/pre>\n<p>9. Under your home directory, create public_html and change owner to www-data. This will be your document root directory :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo mkdir public_html\/\nehowstuff@ubuntu14:~$ sudo chown -R www-data:www-data public_html\/\n<\/pre>\n<p>10. Create index.php under document root directory :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo vim public_html\/index.php\n<\/pre>\n<p>Insert the following :<\/p>\n<pre>\n&lt;?php\nphpinfo();\n?&gt;\n<\/pre>\n<p>11. Now it&#8217;s turn to setup your NGINX. Please modify the default site.<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo vim \/etc\/nginx\/sites-available\/default\n<\/pre>\n<p>Kindly update the root directory and comment out the required lines as below :<\/p>\n<pre>\n# You may add here your\n# server {\n#\t...\n# }\n# statements for each of your virtual hosts to this file\n\n##\n# You should look at the following URL's in order to grasp a solid understanding\n# of Nginx configuration files in order to fully unleash the power of Nginx.\n# http:\/\/wiki.nginx.org\/Pitfalls\n# http:\/\/wiki.nginx.org\/QuickStart\n# http:\/\/wiki.nginx.org\/Configuration\n#\n# Generally, you will want to move this file somewhere, and start with a clean\n# file but keep this around for reference. Or just disable in sites-enabled.\n#\n# Please see \/usr\/share\/doc\/nginx-doc\/examples\/ for more detailed examples.\n##\n\nserver {\n\tlisten 80 default_server;\n\tlisten [::]:80 default_server ipv6only=on;\n\n\troot \/home\/ehowstuff\/public_html;\n\tindex index.html index.htm index.php;\n\n\t# Make site accessible from http:\/\/localhost\/\n\tserver_name localhost;\n\n\tlocation \/ {\n\t\t# First attempt to serve request as file, then\n\t\t# as directory, then fall back to displaying a 404.\n\t\ttry_files $uri $uri\/ =404;\n\t\t# Uncomment to enable naxsi on this location\n\t\t# include \/etc\/nginx\/naxsi.rules\n\t}\n\n\t# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests\n\t#location \/RequestDenied {\n\t#\tproxy_pass http:\/\/127.0.0.1:8080;    \n\t#}\n\n        error_page 404 \/404.html;\n\n\t# redirect server error pages to the static page \/50x.html\n\t#\n\terror_page 500 502 503 504 \/50x.html;\n\tlocation = \/50x.html {\n\t\troot \/usr\/share\/nginx\/html;\n\t}\n\n\t# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000\n\t#\n\tlocation ~ \\.php$ {\n\t\tfastcgi_split_path_info ^(.+\\.php)(\/.+)$;\n\t#\t# NOTE: You should have \"cgi.fix_pathinfo = 0;\" in php.ini\n\t#\n\t#\t# With php5-cgi alone:\n\t\tfastcgi_pass 127.0.0.1:9000;\n\t#\t# With php5-fpm:\n\t#\tfastcgi_pass unix:\/var\/run\/php5-fpm.sock;\n\t\tfastcgi_index index.php;\n\t\tinclude fastcgi_params;\n\t}\n\n\t# deny access to .htaccess files, if Apache's document root\n\t# concurs with nginx's one\n\t#\n\tlocation ~ \/\\.ht {\n\t\tdeny all;\n\t}\n}\n\n\n# another virtual host using mix of IP-, name-, and port-based configuration\n#\n#server {\n#\tlisten 8000;\n#\tlisten somename:8080;\n#\tserver_name somename alias another.alias;\n#\troot html;\n#\tindex index.html index.htm;\n#\n#\tlocation \/ {\n#\t\ttry_files $uri $uri\/ =404;\n#\t}\n#}\n\n\n# HTTPS server\n#\n#server {\n#\tlisten 443;\n#\tserver_name localhost;\n#\n#\troot html;\n#\tindex index.html index.htm;\n#\n#\tssl on;\n#\tssl_certificate cert.pem;\n#\tssl_certificate_key cert.key;\n#\n#\tssl_session_timeout 5m;\n#\n#\tssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;\n#\tssl_ciphers \"HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES\";\n#\tssl_prefer_server_ciphers on;\n#\n#\tlocation \/ {\n#\t\ttry_files $uri $uri\/ =404;\n#\t}\n#}\n<\/pre>\n<p>12. Restart your nginx and php5-fpm service to take effect all changes :<\/p>\n<pre>\nehowstuff@ubuntu14:~$ sudo service nginx restart\nehowstuff@ubuntu14:~$ sudo service php5-fpm restart\n<\/pre>\n<p>13. Test your NGINX site by visit http:\/\/IP-Adress :<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/06\/ubuntu-lemp-300x249.png\" alt=\"ubuntu-lemp\" width=\"300\" height=\"249\" class=\"alignnone size-medium wp-image-5138 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/06\/ubuntu-lemp-300x249.png 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2014\/06\/ubuntu-lemp.png 618w\" data-sizes=\"(max-width: 300px) 100vw, 300px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 300px; --smush-placeholder-aspect-ratio: 300\/249;\" \/><\/p>\n<p>Done!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NGINX, pronounced \u201cEngine X\u201d is an alternate web server for Apache. NGINX is an open source web server and a reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols&#8230;.<\/p>\n","protected":false},"author":6,"featured_media":5142,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1014,1073],"tags":[1536,1605,1652,1827,1859],"class_list":["post-5131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx","category-ubuntu","tag-linux","tag-nginx","tag-php-fpm","tag-tags-how-to","tag-ubuntu-14-04-lts"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5131","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=5131"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/5142"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=5131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=5131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=5131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}