Add php/nginx guide

master
Nathan Steel 4 years ago
parent ec8c40f001
commit e8bb716a16

@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content="Blog, articles, guide, php, nginx">
<meta name="author" content="Nathan (Aney) Steel">
<meta name="theme-color" content="white">
<meta name="theme-color" content="black">
<link rel="stylesheet" type="text/css" href="/main.css">
<link rel="icon" type="image/png" href="/images/favicon.svg">
<title>Adding PHP to NGINX server</title>
</head>
<body>
<header>
<h1>Adding PHP to NGINX server</h1>
<hr/>
<nav>
<a href="/">home</a>
<a href="/equipment.html">equipment</a>
<a href="/projects.html">projects</a>
<a href="/blog/">blog</a>
<a href="/sitemap.html">misc</a>
<a href="/support.html">support</a>
</nav>
<hr/>
</header>
<main>
<section>
<p class="intro">PHP is one of the highest used programming languages for websites, and it allows you to add practically any functionality you'd ever want to your sites.</p>
<h2>Install</h2>
<pre><code>sudo apt install php-fpm php-mysql</code></pre>
<h2>Um, I forgor</h2>
<pre><code>sudo nano /etc/php/<VERSION>/fpm/php.ini</code></pre>
<p>Comment the cgi.fix_pathinfo line, to look like below</p>
<pre><code>#set cgi.fix_pathinfo = 0</code></pre>
<h2>Add to Website's NGINX conf</h2>
<p>For each website you want to use php, you'll need to edit the confige file</p>
<pre><code>sudo vim /etc/nginx/sites-available/<WEBSITE></code></pre>
<p>The following code needs adding within the XXX block</p>
<pre><code>location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php<VERSION>-fpm.sock;
}</pre></code>
<p>This will use nginx's fastcgi-php.conf snippet which is more secure by deafult than many other php/nginx configs because it 404s if the files doesn't exist. Read Neal Poole's<a href="https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/" target="_blank" rel="noopener">Don't trust the tutorials</a> for more info.</p>
<h2>Reload NGINX</h2>
<pre><code>sudo systemctl reload nginx</code></pre>
<h2>Test it works</h2>
<p>Create a PHP file e.g. <strong>filename.php</strong> in the website's directory, and add the snippet below into it</p>
<pre><code><?php phpinfo(); ?></code></pre>
<p>Go to that webpage in your browser e.g. <strong>domain.co.uk/filename.php</strong>, and if php is working you should see a dump of your PHP's version, headers, etc.</p>
<h2>Make nginx use index.php as homepage/root</h2>
<p>Now we'll set nginx to load up index.php as the root of the website, if it exists. Open the site's config with an editor</p>
<pre><code>vim /etc/nginx/sites-available/<WEBSITE></code></pre>
<p>Change the index line to rad as below. This will then tell the server to load index.php, and if it doesn't exists, load index.html in it's stead</p>
<pre><code>index index.php index.html</code><pre>
</section>
</main>
<footer>
<hr/>
<p>Written by <a href="http://aney.co.uk" target="_blank" rel="noopener">@aney</a> with <a href="https://danluu.com/web-bloat/" target="_blank" rel="noopener">web bloat</a> in mind | <a href="https://github.com/Aney/website" target="_blank" rel="noopener">Source Code</a></p>
</footer>
</body>
</html>

@ -35,7 +35,7 @@
<p>To run adminer, you'll need a webserver with php set up. Thankfully I'm a great guy, and have written about these topics before.</p> <p>To run adminer, you'll need a webserver with php set up. Thankfully I'm a great guy, and have written about these topics before.</p>
<ul> <ul>
<li><a href="/blog/nginx-install.html">Nginx Webserver Setup</a></li> <li><a href="/blog/nginx-install.html">Nginx Webserver Setup</a></li>
<li>PHP setup for NGINX</li> <li><a href="/blog/add-php-to-nginx.html">PHP setup for NGINX</a></li>
</ul> </ul>
<h2>Download the latest release</h2> <h2>Download the latest release</h2>

@ -102,6 +102,7 @@
<li>mailserver</li> <li>mailserver</li>
<li>cockpit for noob-friendly server management</li> <li>cockpit for noob-friendly server management</li>
<li>Basic Homeserver for a web developer/designer</li> <li>Basic Homeserver for a web developer/designer</li>
<li>Host client websites</li>
</ul> </ul>
</section> </section>

@ -38,6 +38,7 @@
<h2>2022</h2> <h2>2022</h2>
<ul> <ul>
<li><a href="/blog/add-php-to-nginx.html">PHP on Nginx setup</a> - 10/06/2022</li>
<li><a href="/blog/sql-cheatsheet.html">SQL Cheatsheet</a> - 09/06/2022</li> <li><a href="/blog/sql-cheatsheet.html">SQL Cheatsheet</a> - 09/06/2022</li>
<li><a href="/blog/adminer-setup.html">Adminer Setup</a> - 09/06/2022</li> <li><a href="/blog/adminer-setup.html">Adminer Setup</a> - 09/06/2022</li>
<li><a href="/blog/backup-mysql-mariadb.html">Backing Up MySQL/MariaDB</a> - 09/06/2022</li> <li><a href="/blog/backup-mysql-mariadb.html">Backing Up MySQL/MariaDB</a> - 09/06/2022</li>

Loading…
Cancel
Save