How to Install Nginx and PHP on Debian Server
Install Nginx – If you’re looking to host a website on a Debian server, you’ll need to install a web server and a scripting language. In this article, we’ll cover how to install Nginx and PHP on a Debian server.
Prerequisites
Before we start, you’ll need to have root access to your server and have updated the package list.
Installing Nginx
Nginx is a popular web server that’s known for its high performance and low resource usage. Here’s how to install it:
Step 1: Update the package list
sudo apt update
Step 2: Install Nginx
sudo apt install nginx
Step 3: Start Nginx
sudo systemctl start nginx
Step 4: Verify Nginx is Running
sudo systemctl status nginx
Installing PHP
PHP is a popular scripting language that’s often used for web development. Here’s how to install it:
Step 1: Install PHP and Required Extensions
sudo apt install php-fpm php-mysql
Step 2: Configure PHP
sudo nano /etc/php/7.4/fpm/php.ini
Uncomment the following line by removing the semicolon:
;cgi.fix_pathinfo=1
Save and close the file.
Step 3: Restart PHP
sudo systemctl restart php7.4-fpm
Configuring Nginx for PHP
Now that we have both Nginx and PHP installed, we need to configure Nginx to use PHP. Here’s how:
Step 1: Create a New Server Block
sudo nano /etc/nginx/sites-available/example.com
Replace “example.com” with your domain name.
Step 2: Add the Following Code to the File
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
try_files $uri $uri/ =404;
}location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
Step 3: Enable the Server Block
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Step 4: Test the Configuration
sudo nginx -t
Step 5: Reload Nginx
sudo systemctl reload nginx
Conclusion
In this article, we covered how to install Nginx and PHP on a Debian server. With these tools, you can host a website and serve dynamic content.
FAQs
- What is Nginx? Nginx is a popular web server that’s known for its high performance and low resource usage.
- What is PHP? PHP is a popular scripting language that’s often used for web development.
- How do I start Nginx? You can start Nginx using the following command:
sudo systemctl start nginx
. - How do I restart PHP? You can restart PHP using the following command:
sudo systemctl restart php7.4-fpm
. - How do I test my Nginx configuration? You can test your Nginx configuration using the following command:
sudo nginx -t
.