Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

If you have CGI or other script content, such as a website written in PHP, like
WordPress or Drupal, you have more work to do. While the ability to serve
PHP is available by default in Nginx (it didn’t use to be), it still requires
additional setup. In this section you will add the ability to serve PHP content
to existing Nginx server. As a result, there are some PHP-specific parts to this
process; they should be obvious.


First, you need to make sure PHP is installed on the server. There are many
ways to do this. For this example, you can use PHP-FPM, which you can
learn more about at http://php-fpm.org.


Install the following packages: php5-cli, php5-cgi, psmisc, spawn-
fcgi, and php5-fpm.


Edit the file /etc/php5/fpm/pool.d/www.conf to make php-fpm
use a UNIX socket instead of a TCP/IP connection by finding this line:


Click here to view code image
listen = 127.0.0.1:9000


and replacing it with this:


Click here to view code image
listen = /tmp/php5-fpm.sock


This is where you need to check the requirements of whatever PHP-based
application you intend to install and use. Some require other PHP extensions.
You may also need a database. We will skip this step as the details differ for
each application, but this is when you would likely want to take care of these
details, although you can probably make them up later. If all you want to do is
serve PHP scripts that you have written or run them on your server, you are
probably fine not worrying about this and moving on.


Next, you need to edit either /etc/nginx/nginx.conf or the file for
your virtual host, like /etc/nginx/sites-
enabled/yourdomain.com from the earlier example, to include
information that Nginx needs in order to deal with PHP content. Editing either
will work; the difference is that editing the virtual host file will affect only
that website, while editing nginx.conf will affect everything on your
server.


Here is an example of an edited server module in one of these files. Note
what is moved and what is added from the previous example:


Click here to view code image
server {
listen 80; #sets the HTTP port from which

Free download pdf