Articles on: Dedicated Servers

How to set up NGINX + PHP on your Dedicated Server

This article will show you how to configure an NGINX server to serve PHP applications such as the Multicraft panel. To follow this guide you will first need to SSH into your Dedicated Server


Installing NGINX


To install NGINX you will need to run the following sequence of commands:


  1. yum update -y
  2. yum remove -y httpd - Removes the default Apache webserver
  3. yum install -y epel-release
  4. yum install -y nginx
  5. systemctl enable --now nginx
  6. If your machine uses firewalld, run firewall-cmd --zone=public --permanent --add-service=http; firewall-cmd --zone=public --permanent --add-service=https; firewall-cmd --reload;


After this you should be able to type the machine's IP into your browser to see an NGINX example page


Installing PHP


  1. yum install -y yum-utils
  2. yum-config-manager --enable remi-php73
  3. yum install -y php php-fpm php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd
  4. sed -i 's/user = .*/user = nginx/' /etc/php-fpm.d/www.conf; sed -i 's/group = .*/group = nginx/' /etc/php-fpm.d/www.conf - Fixes the permissions on the webserver
  5. chown -R root:nginx /var/lib/php/session
  6. systemctl enable --now php-fpm
  7. mkdir /etc/nginx/includes
  8. Place the following file as /etc/nginx/includes/php.conf:


location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}


Making your first NGINX config


Each individual website you would like to run, needs to have an NGINX config to serve it, and a folder on the system for the files to go in. In this tutorial we will set up example.pebblehost.com, so replace this for your domain in each command


  1. mkdir -p /var/www/example.pebblehost.com; echo "<?php phpinfo(); ?>" > /var/www/example.pebblehost.com/test.php - Makes the folder and a test PHP file
  2. Make sure you have set up an A Record pointing to your dedicated server's IP, on the domain
  3. Add a file at /etc/nginx/conf.d/example.pebblehost.com.conf with the following contents:


server {
listen 80;
listen [::]:80;
server_name .example.pebblehost.com;
index index.php index.html index.htm;
location / {
root /var/www/example.pebblehost.com;
include includes/php.conf;
}
}


  1. systemctl restart nginx - Allows NGINX to pick up the new configuration file
  2. Browse to http://example.pebblehost.com/test.php in your browser - it should show the test file!
  3. rm -rf /var/www/example.pebblehost.com/test.php - Remove the test file and upload your real website files
  4. chown -R nginx:nginx /var/www - Run this after any file changes to ensure permissions are configured correctly




Updated on: 26/01/2021

Was this article helpful?

Share your feedback

Cancel

Thank you!