Update php/nginx guide

master^2
Nathan Steel 3 years ago
parent b518b94fec
commit 72c8392e70

@ -36,42 +36,38 @@
<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>
<pre><code>sudo apt install php-fpm</code></pre>
<p>If you want to work with mysql, then you also need to install <strong>php-mysql</strong>.
<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>
<p>For each website you want to use php, you'll need to edit the config file.</p>
<pre><code>sudo vim /etc/nginx/sites-available/<WEBSITE></code></pre>
<p>The following code needs adding within the XXX block</p>
<p>The following code needs adding within the <strong>server</strong> block.</p>
<pre><code>location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php<VERSION>-fpm.sock;
fastcgi_pass unix:/run/php/php&lt;VERSION&gt;-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>
<p>This will use nginx's fastcgi-php.conf snippet which is more secure by default than many other php/nginx configs because it 404s if the requested file doesn't exist. Read Neal Poole's <q><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></q> 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>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>&lt;?php phpinfo(); ?&gt;</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>
<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 read 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>
<p>Change the index line to read as below. This will then tell the server to load index.php, and if it doesn't exists, load index.html in its stead.</p>
<pre><code>index index.php index.html</code></pre>
</section>
</main>
<footer>
<hr/>
<p>Written by <a href="https://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>
<p>Written by <a href="https://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>

@ -62,7 +62,7 @@
<li><a href="/guides/setup-nginx-website.html">Setup an nginx website</a></li>
<li><a href="/guides/certbot-ssl.html">Add an SSL certificate w/Certbot</a></li>
<li>TODO:<a href="/guides/nginx-web-optimisation.html">Nginx web optimisation</a></li>
<li>TODO:<a href="/guides/add-php-to-nginx.html">(Optional) Add PHP to your webserver</a></li>
<li><a href="/guides/add-php-to-nginx.html">(Optional) Add PHP to your webserver</a></li>
</ul>
<h2>MariaDB Database</h2>

Loading…
Cancel
Save