The Ultimate Guide To Setting Up Nginx, PHP, and MySQL on a New VPS Running Linux
Setting up a web server on a Virtual Private Server (VPS) is a crucial skill for anyone looking to host websites, web applications, or other online content.
Among the various web server configurations available, the combination of Nginx, PHP, and MySQL (often referred to as LEMP) is a popular choice for its performance, security, and flexibility. In this detailed guide, we’ll take you through the comprehensive process of setting up Nginx, PHP, and MySQL on a new VPS running a Linux distribution.
Step 1: Selecting a VPS Provider
Before diving into the setup process, you need to choose a VPS provider that suits your needs. Popular options include DigitalOcean, Linode, AWS, and Google Cloud.
Factors to consider when selecting a provider include pricing, server locations, available resources, and ease of use. Once you’ve chosen a provider and created an account, you can proceed to set up your VPS.
Step 2: Accessing Your VPS
Most VPS providers offer a web-based control panel or command-line interface for managing your VPS. You’ll need to log in to your provider’s dashboard and create a new virtual server instance (VPS).
During this process, you can choose the server’s location, operating system, and hardware specifications. It’s recommended to select a Linux distribution, such as Ubuntu, CentOS, or Debian, as they are well-supported for web hosting.
Once your VPS is provisioned, you’ll receive SSH access credentials, typically consisting of a username and an SSH key pair. Use a terminal or SSH client to connect to your VPS using the provided credentials.
ssh username@your_vps_ip
Step 3: Update and Secure Your VPS
Before installing any software, it’s crucial to update your VPS to ensure it has the latest security patches and updates. On Debian/Ubuntu-based systems, use the following commands:
sudo apt update
sudo apt upgrade
On CentOS-based systems, use:
sudo yum update
After updating, consider securing your VPS by configuring a firewall (e.g., UFW on Ubuntu/Debian or firewalld on CentOS) to restrict incoming and outgoing traffic. Ensure SSH access is restricted to trusted IP addresses only to enhance security.
Step 4: Installing Nginx
Nginx is a high-performance, open-source web server known for its speed and efficiency. To install Nginx on your VPS, use the package manager for your Linux distribution:
On Ubuntu/Debian:
sudo apt install nginx
On CentOS:
sudo yum install nginx
After installation, start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
By default, Nginx serves a welcome page. You can verify the installation by visiting your VPS’s IP address in a web browser. If you see the Nginx welcome page, Nginx is up and running.
Step 5: Configuring Nginx
Nginx configuration files are located in the /etc/nginx
directory. The main configuration file is nginx.conf
, but server block configurations (similar to Apache virtual hosts) are stored in the /etc/nginx/sites-available
directory. To create a server block for your website or application, follow these steps:
- Create a new configuration file for your site:
sudo nano /etc/nginx/sites-available/your_site_name
- Add a basic server block configuration. Replace
your_site_name
andyour_domain_or_ip
with appropriate values:
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/your_site_name;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust for your PHP version
}
location ~ /\.ht {
deny all;
}
}
- Create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/your_site_name /etc/nginx/sites-enabled/
- Test the Nginx configuration for syntax errors:
sudo nginx -t
- If there are no errors, reload Nginx to apply the configuration:
sudo systemctl reload nginx
Your website or application is now configured to work with Nginx. You can place your web files in the /var/www/your_site_name
directory.
Step 6: Installing PHP
PHP is a server-side scripting language commonly used for web development. To install PHP on your VPS, use the appropriate package manager based on your Linux distribution:
On Ubuntu/Debian:
sudo apt install php-fpm
On CentOS:
sudo yum install php-fpm
After installing PHP, start the PHP-FPM service and enable it to start on boot:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Step 7: Configuring PHP-FPM
PHP-FPM (FastCGI Process Manager) is responsible for handling PHP scripts. To configure PHP-FPM, edit its configuration file:
sudo nano /etc/php/7.4/fpm/php.ini # Adjust the version according to your PHP installation
Inside the php.ini
file, make sure the following settings are configured as needed:
cgi.fix_pathinfo=0
Next, open the PHP-FPM pool configuration file:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf # Adjust the version according to your PHP installation
Locate the listen
directive and ensure it’s set to use a Unix socket:
listen = /var/run/php/php7.4-fpm.sock # Adjust the version according to your PHP installation
Save and exit the file. Then, restart PHP-FPM to apply the changes:
sudo systemctl restart php-fpm
Step 8: Installing and Securing MySQL
MySQL is a popular relational database management system (RDBMS) used for storing and managing data. To install MySQL, use your distribution’s package manager:
On Ubuntu/Debian:
sudo apt install mysql-server
On CentOS:
sudo yum install mysql-server
During the installation process, you will be prompted to set a root password for MySQL. Make sure to choose a strong, secure password.
After installation, start the MySQL service and enable it to start on boot:
sudo systemctl start mysql
sudo systemctl enable mysql
MySQL comes with a security script that can help you secure your installation:
sudo mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow remote root login, and remove the test database. These steps help enhance the security of your MySQL installation.
Step 9: Creating a Database and User
With MySQL
installed and secured, you can now create a database and a user for your web application. Log in to the MySQL command-line interface as the root user:
mysql -u root -p
Enter the root password you set during installation.
Create a new database for your application (replace your_database
with your desired database name):
CREATE DATABASE your_database;
Next, create a dedicated MySQL user and grant it privileges for the new database. Replace your_user
and your_password
with your chosen username and password:
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
Exit the MySQL prompt:
exit;
Your MySQL database and user are now set up and ready to be used by your web application.
Step 10: Testing the LEMP Stack
To ensure that Nginx, PHP, and MySQL are working together correctly, you can create a simple PHP test file and access it through your web browser.
Create a test file (e.g., info.php
) in your website’s root directory:
sudo nano /var/www/your_site_name/info.php
Add the following PHP code to the info.php
file:
<?php
phpinfo();
?>
Save and exit the file.
Now, open your web browser and navigate to http://your_domain_or_ip/info.php
. You should see a page displaying PHP information, which confirms that Nginx, PHP, and MySQL are all working together on your VPS.
Final thoughts
Congratulations! You’ve successfully set up a LEMP (Linux, Nginx, PHP, MySQL) stack on your VPS. You now have a powerful web server environment capable of hosting websites, web applications, and more. This guide covered the entire process, from selecting a VPS provider to testing your server’s functionality.
As you continue to develop and deploy your web projects, you’ll find this LEMP stack to be a robust and reliable foundation for your online ventures.
Credits: Vps icons created by Freepik – Flaticon