{"id":20,"date":"2017-02-15T16:14:00","date_gmt":"2017-02-15T16:14:00","guid":{"rendered":""},"modified":"2021-02-22T01:13:22","modified_gmt":"2021-02-22T01:13:22","slug":"installing-wordpress-on-php7-nginx","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2017\/02\/installing-wordpress-on-php7-nginx.html","title":{"rendered":"Installing WordPress on PHP7, Nginx &#038; Ubuntu 16.04 With SSL Support"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<p>&nbsp;<\/p>\n<div style=\"clear: both; text-align: center;\"><a style=\"margin-left: 1em; margin-right: 1em;\" href=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/nginx-wordpress-logo.png\"><img decoding=\"async\" title=\"Nginx WordPress Logo\" src=\"https:\/\/www.tech-and-dev.com\/blog\/wp-content\/uploads\/2021\/02\/nginx-wordpress-logo.png\" alt=\"Nginx WordPress Logo\" border=\"0\" \/><\/a><\/div>\n<p>This tutorial assumes you already have LEMP installed on Ubuntu 16.04 with PHP7 &amp; PHP-FPM. If you don&#8217;t have anything installed yet, you can find a tutorial on how to do it in <a href=\"https:\/\/www.tech-and-dev.com\/blog\/2017\/01\/23\/installing-lemp-ubuntu-16-04-nginx-mariadb-php-7\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>For this tutorial, I&#8217;ll assume you are logged in as root. Ideally however, you must use a non-root username and prefix all the commands with <b>sudo.<\/b><\/p>\n<h2 style=\"text-align: left;\">Update your packages<\/h2>\n<pre>apt-get update &amp;&amp; apt-get upgrade<\/pre>\n<p><a name=\"more\"><\/a><\/p>\n<h2 style=\"text-align: left;\">Install PHP Packages that WordPress will use<\/h2>\n<pre>apt-get install php-xmlrpc php-xml php-gd php-mbstring php-curl php-mcrypt<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Restart PHP-FPM<\/h4>\n<pre>systemctl restart php7.0-fpm<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Configure Nginx<\/h2>\n<p>I&#8217;ll assume my website is called <span style=\"color: #a64d79;\">mysite.com<\/span> and the configuration file will be called <span style=\"color: #cc0000;\">mysite.com<\/span> as well (you can call them differently)<\/p>\n<p>First, create the directly where the SSL certificate &amp; key will be uploaded (Even if you won&#8217;t use SSL, proceed with this step)<\/p>\n<pre>mkdir \/var\/www\/mysite.com<\/pre>\n<p>And create the directory where wordpress will be uploaded<\/p>\n<pre>mkdir \/var\/www\/mysite.com\/html<\/pre>\n<p>Secondly, create the nginx configuration file:<\/p>\n<pre>vi \/etc\/nginx\/sites-available\/mysite.com<\/pre>\n<p>&nbsp;<\/p>\n<pre>server {<\/pre>\n<p># Force Redirect to www, change server_name according to your needs<\/p>\n<p>server_name mysite.com;<\/p>\n<p>rewrite ^\/(.*)$ http:\/\/www.mysite.com\/$1 permanent;<\/p>\n<p>}<\/p>\n<p>server {<\/p>\n<p># Domain name<br \/>\nserver_name www.mysite.com;<\/p>\n<p># Location of files<br \/>\nroot \/var\/www\/mysite.com\/html;<\/p>\n<p># Location of access &amp; error Logs, you can call them anything you like<br \/>\naccess_log \/var\/log\/nginx\/www.mysite.com.access.log;<br \/>\nerror_log \/var\/log\/nginx\/www.mysite.com.error.log;<\/p>\n<p># Listen to Port 80 (http)<br \/>\nlisten 80;<\/p>\n<p>#Listen on SSL with http2 support<br \/>\nlisten 443 ssl http2 default_server;<br \/>\nssl_certificate \/var\/www\/mysite.com\/ssl.cert;<br \/>\nssl_certificate_key \/var\/www\/mysite.com\/ssl.key;<\/p>\n<p># Default file to serve. If the first file isn&#8217;t found,<br \/>\nindex index.php index.html index.htm;<\/p>\n<p># Don&#8217;t log favicons<br \/>\nlocation = \/favicon.ico {<br \/>\nlog_not_found off;<br \/>\naccess_log off;<br \/>\n}<\/p>\n<p># Configuring robots.txt and disable its logging<br \/>\nlocation = \/robots.txt {<br \/>\nallow all;<br \/>\nlog_not_found off;<br \/>\naccess_log off;<br \/>\n}<\/p>\n<p># Configure 404 Pages<br \/>\nerror_page 404 \/404.html;<\/p>\n<p># Configuring Error 50x Pages<br \/>\nerror_page 500 502 503 504 \/50x.html;<br \/>\nlocation = \/50x.html {<br \/>\nroot \/usr\/share\/nginx\/www;<br \/>\n}<\/p>\n<p># Denying all attempts to access hidden files (example .htaccess)<br \/>\nlocation ~ \/. {<br \/>\ndeny all;<br \/>\n}<\/p>\n<p># Expiry date headers for static files and turn off logging.<br \/>\nlocation ~* ^.+.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {<br \/>\naccess_log off; log_not_found off; expires 30d;<br \/>\n}<\/p>\n<p># Rewrite rules, sends everything through index.php<br \/>\nlocation \/ {<br \/>\ntry_files $uri $uri\/ \/index.php?q=$uri&amp;$args;<br \/>\n}<\/p>\n<p># Deny access to PHP Files in the uploads directory<br \/>\nlocation ~* \/(?:uploads|files)\/.*.php$ {<br \/>\ndeny all;<br \/>\n}<\/p>\n<p># Enable PHP Support<br \/>\nlocation ~ .php$ {<br \/>\ninclude snippets\/fastcgi-php.conf;<br \/>\nfastcgi_pass unix:\/run\/php\/php7.0-fpm.sock;<br \/>\n}<\/p>\n<p># Enable Rewrite Rules for Yoast SEO SiteMap<br \/>\nrewrite ^\/sitemap_index.xml$ \/index.php?sitemap=1 last;<br \/>\nrewrite ^\/([^\/]+?)-sitemap([0-9]+)?.xml$ \/index.php?sitemap=$1&amp;sitemap_n=$2 last;<\/p>\n<p># Add trailing slash to *\/wp-admin requests.<br \/>\nrewrite \/wp-admin$ $scheme:\/\/$host$uri\/ permanent;<br \/>\n}<\/p>\n<p>If you enable listening on port 443, make sure to have the ssl files in their appropriate location (As defined in the configuration file)<\/p>\n<pre>\/var\/www\/mysite.com\/ssl.cert\r\n\/var\/www\/mysite.com\/ssl.key<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Create a link (shortcut) in sites-enabled<\/h4>\n<pre>ln -s \/etc\/nginx\/sites-available\/mysite.com \/etc\/nginx\/sites-enabled\/<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">(Optional) Disable the default server_name by deleting its link<\/h4>\n<pre>unlink \/etc\/nginx\/sites-enabled\/default<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Test nginx settings &amp; syntax<\/h4>\n<pre>nginx -t<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Reload nginx configuration<\/h4>\n<pre>systemctl reload nginx<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Create new Database User and Table<\/h2>\n<h4 style=\"text-align: left;\">Login into database:<\/h4>\n<pre>mysql -u root -p<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Create database table:<\/h4>\n<p>I&#8217;ll call it <b>wpdb<\/b> in this example,<\/p>\n<pre>CREATE DATABASE wpdb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;<\/pre>\n<h4 style=\"text-align: left;\">Create database user and grant the user all the privileges, I&#8217;ll call the user wpuser:<\/h4>\n<pre>GRANT ALL ON wpdb.* TO 'wpuser'@'localhost' IDENTIFIED BY 'password';<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Flush the Database for the changes to take effects immediately:<\/h4>\n<pre>FLUSH PRIVILEGES;<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Exit the Database<\/h4>\n<pre>Exit<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: left;\">Installing WordPress<\/h2>\n<h4 style=\"text-align: left;\">Go to the previously created directory<\/h4>\n<pre>cd \/var\/www\/mysite.com\/html<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Download latest WordPress version<\/h4>\n<pre>https:\/\/wordpress.org\/latest.tar.gz<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Uncompress it<\/h4>\n<pre>gunzip latest.tar.gz<\/pre>\n<p>tar -xvf latest.tar<\/p>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Remove latest.tar<\/h4>\n<pre>rm latest.tar<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">The files will be located in a directory called wordpress, move them outside it<\/h4>\n<pre>mv wordpress\/* .<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Delete the empty WordPress directory<\/h4>\n<pre>rm -rf wordpress<\/pre>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: left;\">Change owner of the files from nobody to www-data<\/h4>\n<pre>chown -R www-data:www-data *<\/pre>\n<p>Install WordPress from the domain name you entered in server_name in the configuration file, your domain must be pointed.<br \/>\nExample: http:\/\/<span style=\"color: blue;\">xxx.xxx.xxx.xxx<\/span><br \/>\nWhere <span style=\"color: blue;\">xxx.xxx.xxx.xxx<\/span> is your server&#8217;s IP address or domain name<\/p>\n<p>Follow the instructions and use the database credentials you created earlier on.<\/p>\n<p><b>Any questions or suggestions, post them below!<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; This tutorial assumes you already have LEMP installed on Ubuntu 16.04 with PHP7 &amp; PHP-FPM. If you don&#8217;t have anything installed yet, you can find a tutorial on how to do it in here. For this tutorial, I&#8217;ll assume you are logged in as root. Ideally however, you must use a non-root username and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,27,4,8],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-linux","category-nginx","category-ubuntu","category-wordpress"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":3,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/379"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}