"<p>Setting up a virtual private server (VPS) can be a daunting task, but with the right guidance and tools, it can be a straightforward process. In this article, we'll walk through the process of setting up a VPS <strong>Ubuntu</strong> using <strong>Nginx</strong> as the web server, <strong>MySQL</strong> as the database server, a <strong>firewall</strong> to secure the server, and <strong>PHP</strong> and <strong>Node.js</strong> for programming.</p> <p>Before we begin, there are a few things you'll need to have:</p> <ul> <li>A Ubuntu VPS</li> <li>Access to a terminal or command-line interface (CLI)</li> <li>A basic understanding of Linux commands</li> </ul> <p><strong>Step 1: Create a new VPS instance</strong></p> <p>The first step is to create a new VPS instance with your provider. This will typically involve selecting a server location, choosing a server size, and setting up your initial user account. Be sure to note the IP address and login credentials for your new server.</p> <p><strong>Step 2: Connect to the server via SSH</strong></p> <p>Once your server is set up, you'll need to connect to it via SSH (Secure Shell) using a terminal or CLI. The command to do this will typically be in the format of <code>ssh user@server_ip</code>, replacing "user" with the username you set up during the VPS creation process, and "server_ip" with the IP address of your server. You may be prompted to enter a password for the initial user account.</p> <p><strong>Step 3: Update and upgrade the server</strong></p> <p>Once you're connected to the server, the first thing you should do is update and upgrade the server. This can be done by running the following commands:</p> <pre><code>sudo apt update sudo apt upgrade </code></pre> <p>This will ensure that your server has the latest security updates and bug fixes.</p> <p><strong>Step 4: Install Nginx</strong></p> <p>Next, we'll install Nginx, which will serve as our web server. This can be done by running the following command:</p> <pre><code>sudo apt install nginx </code></pre> <p>Once the installation is complete, you can start the Nginx service by running the following command:</p> <pre><code>sudo service nginx start </code></pre> <p>You can also check the status of the service by running <code>sudo service nginx status</code>. At this point, you should be able to access your server's IP address in a web browser and see the default Nginx welcome page.</p> <p><strong>Step 5: Install MySQL</strong></p> <p>Now that Nginx is up and running, we can move on to installing MySQL. This can be done by running the following command:</p> <pre><code>sudo apt install mysql-server </code></pre> <p>During the installation process, you'll be prompted to set a password for the MySQL root user. Be sure to choose a strong password and note it down somewhere safe. Once the installation is complete, you can start the MySQL service by running the following command:</p> <pre><code>sudo service mysql start </code></pre> <p>You can also check the status of the service by running <code>sudo service mysql status</code>.</p> <p><strong>Step 6: Install PHP and Node.js</strong></p> <p>Next, we'll install PHP and Node.js, which are the programming languages we'll be using. This can be done by running the following command:</p> <pre><code>sudo apt install php php-fpm nodejs </code></pre> <p>This command will install both PHP and Node.js, along with the necessary dependencies. Once the installation is complete, you can check the version of PHP by running <code>php -v</code> and Node.js by running <code>node -v</code></p> <p><strong>Step 7: Configuring Nginx to work with PHP</strong></p> <p>Finally, we need to configure Nginx to work with PHP.To configure Nginx to work with PHP, we'll need to make a few changes to the default Nginx configuration file. This file is located at <code>/etc/nginx/sites-available/default</code>.</p> <p>First, open the file using a text editor by running the following command:</p> <pre><code>sudo nano /etc/nginx/sites-available/default </code></pre> <p>Next, we need to adjust the server block to include the PHP handler. You will find a <code>location /</code> block in the default file, inside that you need to include the following line:</p> <pre><code>index index.html index.php; </code></pre> <p>Then, you will need to add the following block of code, right after the <code>location /</code> block:</p> <pre><code>location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } </code></pre> <p>Make sure to adjust the PHP version to match the version that you installed.</p> <p>Once you've made these changes, save the file and exit the text editor.</p> <p>Then, you will need to restart the Nginx service to apply the changes by running the following command:</p> <pre><code>sudo service nginx restart </code></pre> <p>Now you should have a basic PHP setup running with Nginx. you can test this by creating a file named <code>info.php</code> in your webroot directory (<code>/var/www/html</code>) and adding the following line of code:</p> <pre><code><?php phpinfo(); ?> </code></pre> <p>And then you can access this file by visiting <code>http://your_server_ip/info.php</code> in your browser.</p> <p>This should display the PHP information page, indicating that PHP is properly configured and running with Nginx.</p> <p>Additionally, you can secure your VPS by implementing a firewall , it's a good security practice to block all incoming traffic except for traffic on ports that are required.</p> <p>ufw, short for Uncomplicated Firewall, is a tool for managing a Linux firewall. It is easy to use and provides a basic set of firewall rules to help secure your VPS.</p> <p>To install ufw on a Ubuntu-based VPS, run the following command:</p> <pre><code>sudo apt install ufw </code></pre> <p>Once ufw is installed, you can check the current status of the firewall by running the command:</p> <pre><code>sudo ufw status </code></pre> <p>You should see that the firewall is inactive and all incoming connections are allowed by default.</p> <p>The first step in configuring ufw is to enable it. You can do this by running the command:</p> <pre><code>sudo ufw enable </code></pre> <p>Once ufw is enabled, you can start adding rules to the firewall. To allow incoming connections on a specific port, use the following command:</p> <pre><code>sudo ufw allow [port number] </code></pre> <p>For example, to allow incoming connections on port 80 (HTTP), you would run the command:</p> <pre><code>sudo ufw allow 80 </code></pre> <p>You can also allow incoming connections for a specific service. For example, to allow incoming connections on port 22 (SSH), you can use the following command:</p> <pre><code>sudo ufw allow ssh </code></pre> <p>Additionally you can allow incoming connections on a specific IP address or range of IP addresses, you can use the following command:</p> <pre><code>sudo ufw allow from [IP address/range] </code></pre> <p>You can also deny incoming connections on a specific port or service by using the "deny" keyword instead of "allow".</p> <p>You can check the status of your ufw configuration any time by running the command:</p> <pre><code>sudo ufw status verbose </code></pre> <p>Once you have added all the desired rules, you can reload the firewall to apply the new rules with the following command:</p> <pre><code>sudo ufw reload </code></pre> <p>It is important to note that, By default, UFW will block all incoming connections and allow all outgoing connections. So if you want to allow all incoming traffic and only want to block specific IP's or ports you can use the default policies as deny for incoming and allow for outgoing.</p> <p>In this way, you have successfully installed and configured ufw on your VPS, it will help to harden the security on your server by only allowing connections on the ports and services that you specify. As always, it's a good idea to regularly review your firewall rules and make sure they are still in line with your security needs.</p> <p>That's it! You should now have a fully-functional VPS running Nginx, MySQL, PHP, Node.js, and a firewall to secure the server. With this setup, you have the capability to host any web application that require Nginx, MySQL, PHP and Node.js as a dependency. If you want to automate this process and deep dive into vps setup and project deploying feel free to take a look at our brand new course <a title="Ansible & Ansistrano" href="courses/ansible-ansistrano" target="_blank" rel="noopener">Ansible & Ansistrano.</a></p>"