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:
yum update -y
yum remove -y httpd
- Removes the default Apache webserveryum install -y epel-release
yum install -y nginx
systemctl enable --now nginx
- 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
yum install -y yum-utils
yum-config-manager --enable remi-php73
yum install -y php php-fpm php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd
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 webserverchown -R root:nginx /var/lib/php/session
systemctl enable --now php-fpm
mkdir /etc/nginx/includes
- 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
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- Make sure you have set up an
A
Record pointing to your dedicated server's IP, on the domain - 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;
}
}
systemctl restart nginx
- Allows NGINX to pick up the new configuration file- Browse to http://example.pebblehost.com/test.php in your browser - it should show the test file!
rm -rf /var/www/example.pebblehost.com/test.php
- Remove the test file and upload your real website fileschown -R nginx:nginx /var/www
- Run this after any file changes to ensure permissions are configured correctly
Updated on: 26/01/2021
Thank you!