VPS Setup and Configuration Tutorial
Overview of Steps
- Purchase VPS and Connect
- Update the System
- Configure the Firewall
- Install LAMP Stack (Linux, Apache, MySQL, PHP)
- Install Common Software (e.g., FTP, PHPMyAdmin)
- Set Up SSH Keys and Disable Root Login
- Set Up Backup Scheme
Step 1: Purchase VPS and Connect
First, you need to purchase a VPS service. Most VPS providers offer various server specifications and prices, and you can choose the configuration that suits your needs. Common VPS providers include:
- LightNode(www.lightnode.com)
- DigitalOcean(www.digitalocean.com)
- Vultr(www.vultr.com)
After purchasing the VPS, you will receive an IP address and root user credentials. Connect to your VPS using SSH.
Connecting to Your VPS:
ssh root@your-vps-ip
Replace your-vps-ip with your VPS's public IP address.
Step 2: Update the System
After logging in, the first thing you should do is ensure your system is up-to-date. Run the following command to update the system:
sudo apt update && sudo apt upgrade -y
This will update all packages and security patches to ensure the system is current.
Step 3: Configure the Firewall
To secure your VPS, configuring a firewall is essential. We will use ufw (Uncomplicated Firewall) to manage firewall rules.
Configuring Firewall Rules
Allow SSH access:
sudo ufw allow ssh
Allow HTTP and HTTPS access (if you plan to run a website):
sudo ufw allow http
sudo ufw allow https
Enable the firewall:
sudo ufw enable
Check the firewall status to confirm the rules are applied:
sudo ufw status
Step 4: Install the LAMP Stack
The LAMP stack is a popular web server environment consisting of Linux, Apache, MySQL, and PHP.
1. Install Apache Web Server
Apache is a widely used web server that runs PHP and other web applications. Install Apache with the following command:
sudo apt install apache2 -y
Once installed, start Apache and enable it to start on boot:
sudo systemctl start apache2
sudo systemctl enable apache2
You can verify Apache is working by visiting your VPS's public IP in a web browser (e.g., http://your-vps-ip). You should see the default Apache welcome page.
2. Install MySQL Database
MySQL is used to store the data for web applications. Install MySQL with:
sudo apt install mysql-server -y
Once installed, run the MySQL secure installation script:
sudo mysql_secure_installation
This command will prompt you to set the MySQL root password and configure other security settings.
3. Install PHP
PHP is a server-side scripting language commonly used for dynamic web pages. Install PHP and the necessary PHP-MySQL module:
sudo apt install php libapache2-mod-php php-mysql -y
Once installed, restart Apache to apply the changes:
sudo systemctl restart apache2
You can test if PHP is working by creating an info.php file:
sudo nano /var/www/html/info.php
Add the following code to the file:
<?php
phpinfo();
?>
Save and close the file, then visit http://your-vps-ip/info.php in your browser. If you see the PHP configuration page, PHP is working correctly.
Step 5: Install Common Software
1. Install FTP Server
If you need to upload files to your VPS, it is recommended to install an FTP server. We will install vsftpd, a popular FTP server.
To install vsftpd, run:
sudo apt install vsftpd -y
Start the service and enable it to start at boot:
sudo systemctl start vsftpd
sudo systemctl enable vsftpd
Modify the configuration file /etc/vsftpd.conf to allow anonymous or user logins:
sudo nano /etc/vsftpd.conf
After editing, restart vsftpd:
sudo systemctl restart vsftpd
2. Install PHPMyAdmin
If you need a web interface to manage MySQL databases, you can install PHPMyAdmin.
To install PHPMyAdmin, run:
sudo apt install phpmyadmin -y
Configure Apache to support PHPMyAdmin:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Now, you can access PHPMyAdmin through http://your-vps-ip/phpmyadmin.
Step 6: Set Up SSH Keys and Disable Root Login
To enhance security, it is recommended to use SSH key-based login instead of password login.
Generate an SSH key pair on your local machine (if you haven't already):
ssh-keygen -t rsa -b 2048
Copy the public key to your VPS:
ssh-copy-id root@your-vps-ip
Disable root login via password by editing the SSH configuration file:
sudo nano /etc/ssh/sshd_config
Find and modify the following lines:
PermitRootLogin no
PasswordAuthentication no
Save and close the file, then restart the SSH service:
sudo systemctl restart ssh
Step 7: Set Up Backup Scheme
To avoid data loss, you should regularly back up your VPS. You can use tools like rsync, rclone, or Bacula for backup, or you can choose automated backup services like Backblaze B2 or AWS S3.
Create a simple backup script to back up important directories like /var/www and /etc:
#!/bin/bash
tar -czvf /path/to/backup/website_backup_$(date +\%F).tar.gz /var/www /etc
You can add this script to your crontab to run periodically:
crontab -e
Add the following line to run a backup every day:
0 2 * * * /path/to/backup/script.sh
Summary
You have successfully set up a basic VPS, installed common software, and applied some security configurations. You can further configure and optimize your server based on your needs, install additional services like mail servers or reverse proxies, and ensure regular backups and updates. If you have any questions or need further assistance, feel free to ask!