DEV Community

Samuel Grant
Samuel Grant

Posted on

A Complete Guide to Deploying a Website on a VPS

1. Connect to Your VPS

First, you need to connect to your VPS server via SSH.

In the terminal, enter the following command (replace <your-vps-ip> with your VPS IP address):

ssh root@<your-vps-ip>
Enter fullscreen mode Exit fullscreen mode

If it's your first time connecting, the system will ask you to confirm the connection. Type yes and press Enter.

2. Update the Server

Before installing any software, make sure all the software packages on your server are up to date:

apt update && apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

3. Install a Web Server (Nginx or Apache)

You can choose either Nginx or Apache as your web server. Below are the steps for installation and configuration.

Install Nginx:

apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

Install Apache:

apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

Once installed, you can start and enable Nginx or Apache with the following commands:

systemctl start nginx  # Start Nginx
systemctl enable nginx  # Enable Nginx to start on boot
Enter fullscreen mode Exit fullscreen mode

or

systemctl start apache2  # Start Apache
systemctl enable apache2  # Enable Apache to start on boot
Enter fullscreen mode Exit fullscreen mode

4. Install PHP (if needed)

If your website is built using PHP (e.g., WordPress), you will need to install PHP and its extensions.

Install PHP and common extensions:

apt install php-fpm php-mysql php-cli php-xml php-curl php-mbstring php-zip -y
Enter fullscreen mode Exit fullscreen mode

5. Install MySQL (if needed)

If your website requires a database (e.g., WordPress uses MySQL), you will need to install MySQL:

apt install mysql-server -y
Enter fullscreen mode Exit fullscreen mode

Once installed, start MySQL with the following command:

systemctl start mysql
systemctl enable mysql
Enter fullscreen mode Exit fullscreen mode

Configure MySQL (set root password, create databases, etc.):

mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

Then log in to MySQL:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

Create a new database and user:

CREATE DATABASE mywebsite;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mywebsite.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Enter fullscreen mode Exit fullscreen mode

6. Configure the Web Server

For Nginx:

  1. Create a new site configuration file in /etc/nginx/sites-available/:
nano /etc/nginx/sites-available/mywebsite
Enter fullscreen mode Exit fullscreen mode
  1. Example configuration file (replace /var/www/html with the directory where your website files are stored):
server {
    listen 80;
    server_name yourdomain.com;  # Replace with your domain

    root /var/www/mywebsite;  # Directory where your website files are located
    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;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create a symbolic link to enable the site:
ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode
  1. Check if the Nginx configuration is correct:
nginx -t
Enter fullscreen mode Exit fullscreen mode
  1. Reload Nginx to apply the changes:
systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

For Apache:

  1. Create a new site configuration file in /etc/apache2/sites-available/:
nano /etc/apache2/sites-available/mywebsite.conf
Enter fullscreen mode Exit fullscreen mode
  1. Example configuration file:
<VirtualHost *:80>
    ServerAdmin webmaster@yourdomain.com
    ServerName yourdomain.com  # Replace with your domain
    DocumentRoot /var/www/mywebsite  # Directory where your website files are located

    <Directory /var/www/mywebsite>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode
  1. Enable the site and reload Apache:
a2ensite mywebsite.conf
systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

7. Upload Website Files

Upload your website files to the VPS. You can use SFTP, FTP, or command-line tools like scp.

For example, using scp to upload your website files to /var/www/mywebsite:

scp -r /local/path/to/website/* root@<your-vps-ip>:/var/www/mywebsite/
Enter fullscreen mode Exit fullscreen mode

8. Configure the Domain Name

  1. Log in to your domain registrar’s control panel.
  2. Go to the DNS settings and set the A record to point to your VPS IP address.

For example, if your VPS IP is <your-vps-ip>, set the A record for yourdomain.com to:

yourdomain.com A <your-vps-ip>
Enter fullscreen mode Exit fullscreen mode

9. Test the Website

In your browser, visit your domain (e.g., yourdomain.com), and you should see your uploaded website.

If you are using a PHP website (e.g., WordPress), make sure you have correctly configured the database connection and completed the installation process.

10. Set Up SSL (Optional)

To improve security, you can install an SSL certificate for your website. Using Let’s Encrypt, you can enable SSL for free.

Install Certbot (Let’s Encrypt tool):

apt install certbot python3-certbot-nginx  # For Nginx
Enter fullscreen mode Exit fullscreen mode

or

apt install certbot python3-certbot-apache  # For Apache
Enter fullscreen mode Exit fullscreen mode

Then, request the SSL certificate:

certbot --nginx -d yourdomain.com  # For Nginx
Enter fullscreen mode Exit fullscreen mode

or

certbot --apache -d yourdomain.com  # For Apache
Enter fullscreen mode Exit fullscreen mode

11. Auto-Renew SSL Certificates

Let’s Encrypt certificates are valid for 90 days. You can set up auto-renewal:

crontab -e
Enter fullscreen mode Exit fullscreen mode

Add the following line:

0 0,12 * * * certbot renew --quiet
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is the basic process for deploying a website on a VPS. You can further customize and optimize your server setup, such as configuring a firewall, setting up regular backups, and more. For beginners, it's recommended to try hourly billing VPS options, like LightNode and Vultr, to reduce costs.

Top comments (0)