1. Choosing the Right Linux VPS
When setting up a website, selecting the right Linux VPS (Virtual Private Server) is the first and most crucial step. A VPS gives you dedicated resources, enhanced security, and full control over server configurations, making it a superior choice compared to shared hosting.
Factors to Consider When Choosing a Linux VPS
Before purchasing a Linux VPS, consider the following key factors:
- CPU & RAM: The processing power and memory determine how efficiently your server can handle traffic. If you expect high traffic or run resource-intensive applications, opt for a VPS with higher CPU cores and RAM.
- Storage Type: SSD (Solid State Drive) storage provides faster performance than traditional HDDs. Ensure your VPS uses SSDs for optimal speed.
- Bandwidth & Network Speed: Higher bandwidth allows your website to handle more visitors without slowing down. Look for VPS providers offering unlimited or high-bandwidth plans.
- Operating System (OS): Common Linux distributions include:
- Ubuntu – User-friendly and widely used.
- CentOS – Stable and secure, often used for enterprise servers.
- Debian – Lightweight and reliable for web hosting.
- AlmaLinux/Rocky Linux – Great alternatives to CentOS for long-term stability.
- Scalability: Choose a VPS provider that allows you to upgrade resources easily as your website grows.
- Pricing & Support: Compare pricing plans and ensure the provider offers responsive customer support.
Recommended Linux VPS Providers
For a reliable and high-performance Linux VPS, consider 99RDP’s Linux VPS. They provide:
- SSD-powered VPS for fast performance.
- Various Linux distributions to choose from.
- Flexible pricing plans to suit different needs.
- 24/7 customer support for technical assistance.
Choosing the Right Linux OS for Your Website
Depending on your use case, different Linux distributions are better suited:
- For beginners: Ubuntu or Debian.
- For enterprise websites: CentOS or AlmaLinux.
- For developers: Debian or Arch Linux (if advanced customization is needed).
Once you have selected the right VPS plan, the next step is to connect to your server and start configuring it.
2. Connecting to Your VPS
Once you have purchased a Linux VPS, the next step is to connect to it and start setting up your web server. The most common method to access a Linux VPS remotely is via SSH (Secure Shell Protocol). SSH allows you to securely control your server from your local computer.
How to Access Your VPS via SSH
To connect to your VPS using SSH, you need the following:
- Your VPS IP Address (provided by your hosting provider).
- Username & Password (or SSH key for authentication).
- An SSH Client (Terminal for Linux/macOS, PuTTY for Windows).
Step 1: Find Your VPS Login Details
After purchasing a Linux VPS from 99RDP, you will receive an email containing:
- IP Address (e.g.,
192.168.1.100) - Username (usually
root) - Password (or instructions for setting up SSH keys)
Step 2: Connecting to the VPS
For Linux/macOS Users:
-
Open Terminal on your local machine.
-
Run the following SSH command:
ssh root@your-vps-ipExample:
ssh root@192.168.1.100 -
If prompted, type "yes" to accept the connection.
-
Enter your VPS password when asked.
-
You are now logged into your VPS.
For Windows Users (Using PuTTY):
- Download PuTTY from https://www.putty.org.
- Open PuTTY and enter your VPS IP Address under Host Name (or IP Address).
- Set the Port to
22(default SSH port). - Click Open.
- A security alert may appear. Click Yes to proceed.
- When prompted, enter your username (
root) and password.
Tip: If you are using an SSH key instead of a password, you need to specify the private key file in PuTTY.
Step 3: Secure Your SSH Connection (Optional but Recommended)
For security reasons, consider:
- Changing the default SSH port (
22) to a custom port. - Disabling root login and creating a new user.
- Enabling fail2ban to prevent brute-force attacks.
Common SSH Commands
Here are some essential SSH commands to help you navigate your VPS:
| Command | Description |
|---|---|
ls |
List files in a directory |
cd /path |
Change directory |
pwd |
Show current directory path |
mkdir newfolder |
Create a new folder |
rm filename |
Delete a file |
nano filename |
Edit a file using Nano editor |
htop |
Monitor system resources |
Next Steps: Installing a Web Server
Now that you have successfully connected to your VPS, it’s time to install a web server to host your website.
3. Setting Up a Web Server on Your Linux VPS
Now that you’ve successfully connected to your Linux VPS, the next step is to install and configure a web server. A web server is responsible for processing and serving website requests to users over the internet.
The two most popular web servers for Linux are:
- Apache – A powerful and widely used web server with a simple configuration.
- Nginx – A high-performance, lightweight web server, better suited for handling large amounts of traffic.
Choosing the Right Web Server
- If you need an easy-to-configure server and are running a small-to-medium website, go with Apache.
- If you need better performance, lower memory usage, and faster static content delivery, choose Nginx.
- For optimal performance, you can use Nginx as a reverse proxy in front of Apache.
Installing Apache on Linux VPS
Apache is one of the most widely used web servers. To install it, follow these steps:
Step 1: Update System Packages
Before installing any software, update your package lists using:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo yum update -y # CentOS/RHEL
Step 2: Install Apache
Run the following command based on your Linux distribution:
-
Ubuntu/Debian:
sudo apt install apache2 -y -
CentOS/RHEL:
sudo yum install httpd -y
Step 3: Start and Enable Apache
After installation, start the Apache service and enable it to run at boot:
-
Ubuntu/Debian:
sudo systemctl start apache2 sudo systemctl enable apache2 -
CentOS/RHEL:
sudo systemctl start httpd sudo systemctl enable httpd
Step 4: Allow Apache Through the Firewall
If you have a firewall enabled, allow HTTP and HTTPS traffic:
sudo ufw allow 'Apache Full' # Ubuntu/Debian
sudo firewall-cmd --add-service=http --permanent # CentOS
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
Step 5: Verify Apache Installation
Once installed, check if Apache is running by visiting your VPS IP Address in a web browser:
http://your-vps-ip
If Apache is working correctly, you should see the Apache default page.
Installing Nginx on Linux VPS
If you prefer to use Nginx, follow these steps:
Step 1: Update System Packages
Run:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo yum update -y # CentOS/RHEL
Step 2: Install Nginx
Run the following command:
-
Ubuntu/Debian:
sudo apt install nginx -y -
CentOS/RHEL:
sudo yum install nginx -y
Step 3: Start and Enable Nginx
Run the following to start and enable Nginx:
-
Ubuntu/Debian:
sudo systemctl start nginx sudo systemctl enable nginx -
CentOS/RHEL:
sudo systemctl start nginx sudo systemctl enable nginx
Step 4: Allow Nginx Through the Firewall
If a firewall is enabled, allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full' # Ubuntu/Debian
sudo firewall-cmd --add-service=http --permanent # CentOS
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
Step 5: Verify Nginx Installation
Visit your VPS IP Address in a browser:
http://your-vps-ip
You should see the Nginx welcome page, confirming a successful installation.
Next Steps: Installing a Database System
With your web server up and running, the next step is to install a database management system such as MySQL or MariaDB to store dynamic website data.
4. Installing a Database Management System (MySQL/MariaDB) on Your Linux VPS
Now that your web server (Apache or Nginx) is up and running, the next step is to install a database management system (DBMS) to store and manage your website's data. Most websites, especially those using WordPress, Joomla, or custom PHP applications, require a database to store content, user data, and configurations.
The two most popular open-source database systems are:
- MySQL – The most widely used database for web applications.
- MariaDB – A fork of MySQL that offers better performance and open-source licensing.
Both MySQL and MariaDB use the Structured Query Language (SQL) and work seamlessly with PHP-based applications.
Installing MySQL on Linux VPS
Follow these steps to install MySQL on your VPS.
Step 1: Update System Packages
Before installing MySQL, update your system packages:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo yum update -y # CentOS/RHEL
Step 2: Install MySQL Server
Run the following command based on your Linux distribution:
-
Ubuntu/Debian:
sudo apt install mysql-server -y -
CentOS/RHEL:
sudo yum install mysql-server -y
Step 3: Start and Enable MySQL
After installation, start the MySQL service and enable it to run at boot:
-
Ubuntu/Debian:
sudo systemctl start mysql sudo systemctl enable mysql -
CentOS/RHEL:
sudo systemctl start mysqld sudo systemctl enable mysqld
Step 4: Secure MySQL Installation
MySQL comes with a script that helps you secure your database:
sudo mysql_secure_installation
During the process:
- Set a strong root password.
- Remove anonymous users.
- Disallow remote root login.
- Remove test databases.
- Reload privileges.
Step 5: Verify MySQL Installation
To check if MySQL is running, use:
sudo systemctl status mysql
To log into the MySQL console:
mysql -u root -p
Enter your root password when prompted.
Installing MariaDB on Linux VPS
MariaDB is a popular alternative to MySQL, offering better performance in some scenarios.
Step 1: Install MariaDB Server
-
Ubuntu/Debian:
sudo apt install mariadb-server -y -
CentOS/RHEL:
sudo yum install mariadb-server -y
Step 2: Start and Enable MariaDB
-
Ubuntu/Debian:
sudo systemctl start mariadb sudo systemctl enable mariadb -
CentOS/RHEL:
sudo systemctl start mariadb sudo systemctl enable mariadb
Step 3: Secure MariaDB
Run:
sudo mysql_secure_installation
Follow the prompts similar to MySQL.
Step 4: Verify MariaDB Installation
To check if MariaDB is running:
sudo systemctl status mariadb
To log into the MariaDB console:
mysql -u root -p
Testing Your Database Connection
To ensure that MySQL or MariaDB is working correctly, create a test database:
CREATE DATABASE test_db;
SHOW DATABASES;
If you see test_db listed, your database is working properly.
5. Installing PHP for Dynamic Website Functionality
Now that we have a web server (Apache/Nginx) and a database system (MySQL/MariaDB) set up, the next step is to install PHP. PHP is a widely used server-side scripting language essential for running dynamic websites, including WordPress, Joomla, Drupal, and custom applications.
Why Do You Need PHP?
- Processes dynamic content in web applications.
- Works seamlessly with databases like MySQL and MariaDB.
- Essential for running CMS platforms such as WordPress.
- Compatible with both Apache and Nginx.
Installing PHP on Linux VPS
Step 1: Update System Packages
Ensure your system is up to date before installation:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo yum update -y # CentOS/RHEL
Step 2: Install PHP and Required Modules
Run the following command based on your Linux distribution:
For Apache Users
-
Ubuntu/Debian:
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y -
CentOS/RHEL:
sudo yum install php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
For Nginx Users
Nginx does not process PHP directly; instead, we need to install PHP-FPM (FastCGI Process Manager).
-
Ubuntu/Debian:
sudo apt install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y -
CentOS/RHEL:
sudo yum install php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
Step 3: Start and Enable PHP Service
Once PHP is installed, enable and start the PHP service:
-
For Apache (Ubuntu/Debian):
sudo systemctl restart apache2 -
For Apache (CentOS/RHEL):
sudo systemctl restart httpd -
For Nginx:
sudo systemctl start php-fpm sudo systemctl enable php-fpm sudo systemctl restart nginx
Step 4: Verify PHP Installation
To confirm PHP is installed and working, create a test PHP file:
sudo nano /var/www/html/info.php
Add the following content:
<?php
phpinfo();
?>
Save and exit (CTRL + X, then Y, then ENTER).
Now, open a web browser and go to:
http://your-vps-ip/info.php
If PHP is working, you will see a detailed PHP information page.
Important: After testing, delete the file for security reasons:
sudo rm /var/www/html/info.php
Configuring PHP for Better Performance
For better performance and security, adjust PHP settings in the php.ini file:
-
Open the PHP configuration file:
sudo nano /etc/php/*/apache2/php.ini # For Apache (Ubuntu/Debian) sudo nano /etc/php/*/fpm/php.ini # For Nginx (Ubuntu/Debian) sudo nano /etc/php.ini # For CentOS/RHEL -
Adjust the following settings:
- Increase memory_limit:
memory_limit = 256M - Increase upload_max_filesize:
upload_max_filesize = 64M - Increase post_max_size:
post_max_size = 64M - Enable display_errors for debugging:
display_errors = On
- Increase memory_limit:
-
Save the file and restart the web server:
sudo systemctl restart apache2 # Apache sudo systemctl restart nginx # Nginx
6. Configuring Your Domain Name for Your Linux VPS
Now that your web server (Apache/Nginx), database (MySQL/MariaDB), and PHP are set up, the next step is to configure your domain name. This will allow visitors to access your website using a user-friendly URL instead of an IP address.
Why Configure a Domain Name?
- Makes your website accessible via www.yourdomain.com instead of an IP.
- Improves SEO and branding.
- Allows for SSL (HTTPS) encryption for security.
Step 1: Register a Domain Name
If you haven’t already, purchase a domain name from a domain registrar like:
- Namecheap (https://www.namecheap.com)
- GoDaddy (https://www.godaddy.com)
- Google Domains (https://domains.google)
Once purchased, you will need to configure its DNS (Domain Name System) settings to point to your Linux VPS.
Step 2: Find Your VPS IP Address
Run the following command on your VPS to find its public IP address:
curl ifconfig.me
Example output:
192.168.1.100
Take note of this IP address, as you will need it in the next step.
Step 3: Configure DNS Settings
To link your domain to your VPS, you need to update its DNS settings.
-
Go to Your Domain Registrar's DNS Settings
- Log into your domain provider’s account.
- Find the DNS Management or Nameservers section.
-
Update the A Record
- Look for an existing A Record (or add a new one).
- Set the Host to
@(orwwwforwww.yourdomain.com). - Set the Value to your VPS IP address (e.g.,
192.168.1.100). - Save the changes.
-
(Optional) Add a CNAME Record for WWW
If you wantwww.yourdomain.comto also work, add a CNAME Record:- Set Host to
www. - Set Value to
yourdomain.com. - Save the changes.
- Set Host to
-
Wait for DNS Propagation
- DNS changes can take a few minutes to 24 hours to fully propagate.
- Check if your domain is pointing to your VPS using this command:
nslookup yourdomain.com - You can also use online tools like https://www.whatsmydns.net to check propagation status.
Step 4: Configure Your Web Server to Recognize the Domain
Now that your domain is pointing to your VPS, configure Apache or Nginx to handle requests for your domain.
For Apache Users:
- Create a Virtual Host Configuration File
sudo nano /etc/apache2/sites-available/yourdomain.conf - Add the following configuration:
<VirtualHost *:80> ServerAdmin admin@yourdomain.com ServerName yourdomain.com ServerAlias www.yourdomain.com DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> - Enable the Configuration and Restart Apache
sudo a2ensite yourdomain.conf sudo systemctl restart apache2
For Nginx Users:
- Create a Server Block Configuration File
sudo nano /etc/nginx/sites-available/yourdomain - Add the following configuration:
server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/html; index index.html index.php; location / { try_files $uri $uri/ =404; } } - Enable the Configuration and Restart Nginx
sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled/ sudo systemctl restart nginx
Step 5: Verify Your Domain Setup
Once the DNS has propagated and the web server is configured:
- Open a web browser and go to http://yourdomain.com.
- If the website loads successfully, your domain is correctly configured.
7. Setting Up a Free SSL Certificate (HTTPS) on Your Linux VPS
Now that your domain name is configured, the next step is to secure your website with HTTPS using a free SSL certificate from Let’s Encrypt. An SSL certificate encrypts data between your website and visitors, improving security and SEO rankings.
Why Use Let’s Encrypt for SSL?
- Free: No cost for basic SSL certificates.
- Automatic Renewal: Certificates auto-renew every 90 days.
- Trusted by Browsers: Works on all major web browsers.
- Improves SEO: Google ranks HTTPS websites higher.
Step 1: Install Certbot (Let’s Encrypt Client)
Certbot automates the process of obtaining and renewing SSL certificates.
For Apache Users:
- Ubuntu/Debian:
sudo apt install certbot python3-certbot-apache -y - CentOS/RHEL:
sudo yum install certbot python3-certbot-apache -y
For Nginx Users:
- Ubuntu/Debian:
sudo apt install certbot python3-certbot-nginx -y - CentOS/RHEL:
sudo yum install certbot python3-certbot-nginx -y
Step 2: Obtain an SSL Certificate
Certbot will generate and install an SSL certificate for your domain.
For Apache:
Run the following command:
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
For Nginx:
Run the following command:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Note: Replace
yourdomain.comwith your actual domain.
Step 3: Follow the On-Screen Instructions
Certbot will:
- Ask for your email address (for renewal reminders).
- Ask if you want to redirect HTTP to HTTPS (Choose "Yes" to force HTTPS).
- Install and configure the SSL certificate automatically.
If successful, you should see:
Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Step 4: Restart Web Server to Apply Changes
After installing SSL, restart your web server:
- For Apache:
sudo systemctl restart apache2 - For Nginx:
sudo systemctl restart nginx
Step 5: Verify SSL Installation
Open your web browser and visit:
https://yourdomain.com
If the SSL certificate is correctly installed, your website should now load with HTTPS and a padlock icon in the address bar.
Alternatively, you can test your SSL installation using:
https://www.ssllabs.com/ssltest/
Step 6: Enable Auto-Renewal for SSL Certificate
Let’s Encrypt certificates expire every 90 days, but Certbot can automatically renew them.
Check if the renewal process is working:
sudo certbot renew --dry-run
If successful, Certbot will renew SSL automatically. You can also add a cron job to automate the renewal process.
8. Deploying Your Website on a Linux VPS
Now that your web server (Apache/Nginx), database (MySQL/MariaDB), PHP, and SSL are set up, it’s time to deploy your website. This section covers different methods of uploading website files, setting up a CMS like WordPress, and configuring virtual hosts for multiple websites.
Step 1: Upload Your Website Files
There are three common ways to transfer files to your Linux VPS:
1. Using SFTP (Secure File Transfer Protocol)
SFTP allows you to transfer files securely using an FTP client like FileZilla.
How to Connect Using FileZilla
- Download & Install FileZilla (https://filezilla-project.org/).
- Open FileZilla and go to File > Site Manager.
- Click New Site and enter the following details:
- Protocol: SFTP - SSH File Transfer Protocol
- Host: Your VPS IP Address
- Username:
rootor your SSH user - Password: Your SSH password
- Port:
22
- Click Connect and transfer your website files to the web root directory:
- For Apache:
/var/www/html/ - For Nginx:
/usr/share/nginx/html/
- For Apache:
2. Using SCP (Secure Copy) Command
If you prefer using the command line, use SCP to copy files from your local machine to your VPS:
scp -r /path/to/local/files/ user@your-vps-ip:/var/www/html/
3. Using Git (For Developers)
If your website files are stored in GitHub/GitLab, you can clone them directly onto your VPS:
cd /var/www/html/
git clone https://github.com/your-repository.git
Step 2: Set Correct File Permissions
After uploading files, adjust ownership and permissions:
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/
www-datais the default Apache/Nginx user.- This ensures that your web server can access the files properly.
Step 3: Setting Up WordPress (Optional)
If you want to install WordPress, follow these steps:
1. Download WordPress
cd /var/www/html/
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
mv wordpress/* .
rm -rf wordpress latest.tar.gz
2. Configure the WordPress Database
Login to MySQL:
mysql -u root -p
Run the following SQL commands:
CREATE DATABASE wordpress_db;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
3. Configure WordPress
Rename the sample configuration file:
cp wp-config-sample.php wp-config.php
nano wp-config.php
Modify these lines with your database details:
define('DB_NAME', 'wordpress_db');
define('DB_USER', 'wordpress_user');
define('DB_PASSWORD', 'strongpassword');
define('DB_HOST', 'localhost');
Save and exit (CTRL + X, then Y, then ENTER).
4. Restart Your Web Server
sudo systemctl restart apache2 # For Apache
sudo systemctl restart nginx # For Nginx
5. Complete WordPress Installation
Visit:
http://yourdomain.com
Follow the on-screen instructions to set up WordPress.
Step 4: Configuring Virtual Hosts (For Multiple Websites)
If you want to host multiple websites on the same VPS, configure Virtual Hosts (Apache) or Server Blocks (Nginx).
For Apache:
- Create a new virtual host file:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf - Add the following configuration:
<VirtualHost *:80> ServerAdmin admin@yourdomain.com ServerName yourdomain.com ServerAlias www.yourdomain.com DocumentRoot /var/www/yourdomain.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> - Enable the site and restart Apache:
sudo a2ensite yourdomain.com.conf sudo systemctl restart apache2
For Nginx:
- Create a new server block:
sudo nano /etc/nginx/sites-available/yourdomain.com - Add the following configuration:
server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourdomain.com; index index.html index.php; location / { try_files $uri $uri/ =404; } } - Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/ sudo systemctl restart nginx
Step 5: Test Your Website
- Open a web browser and go to:
http://yourdomain.com - If the website loads correctly, your setup is complete!
9. Securing Your Linux VPS
Now that your website is live, it’s crucial to secure your Linux VPS against cyber threats. Hackers often target VPS servers with brute force attacks, malware, and DDoS attacks. Implementing strong security measures helps protect your data, website, and users.
Step 1: Create a Non-Root User
By default, VPS providers give you root access, but using the root user for everyday tasks is risky. Instead, create a new user with administrative privileges.
- Create a new user (replace
yourusernamewith your preferred username):sudo adduser yourusername - Give the new user sudo privileges:
sudo usermod -aG sudo yourusername - Switch to the new user:
su - yourusername
Now, instead of logging in as root, use yourusername.
Step 2: Disable Root SSH Access
To prevent hackers from trying to log in as root, disable root SSH login.
- Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config - Find the line:
Change it to:PermitRootLogin yesPermitRootLogin no - Restart SSH:
sudo systemctl restart sshd
Now, you must log in using yourusername instead of root.
Step 3: Change Default SSH Port
Hackers frequently scan for servers running SSH on port 22. Changing this port reduces attack attempts.
- Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config - Find the line:
Change it to:#Port 22
(ReplacePort 22222222with any unused port.) - Restart SSH:
sudo systemctl restart sshd - Update your firewall to allow the new SSH port:
sudo ufw allow 2222/tcp # Replace 2222 with your new port
Important: When connecting via SSH, use the new port:
ssh yourusername@your-vps-ip -p 2222
Step 4: Set Up a Firewall (UFW or Firewalld)
A firewall blocks unwanted traffic, protecting your server.
For Ubuntu/Debian (Using UFW)
- Allow essential services:
sudo ufw allow OpenSSH sudo ufw allow http sudo ufw allow https - Enable the firewall:
sudo ufw enable - Check firewall status:
sudo ufw status
For CentOS/RHEL (Using Firewalld)
- Allow essential services:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --reload - Check firewall status:
sudo firewall-cmd --list-all
Step 5: Install Fail2Ban to Prevent Brute-Force Attacks
Fail2Ban monitors login attempts and bans IP addresses that fail too many times.
Install Fail2Ban
- Ubuntu/Debian:
sudo apt install fail2ban -y - CentOS/RHEL:
sudo yum install fail2ban -y
Enable and Configure Fail2Ban
- Copy the default config file:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local - Open the config file:
sudo nano /etc/fail2ban/jail.local - Modify these settings:
bantime = 3600 findtime = 600 maxretry = 3 - Restart Fail2Ban:
sudo systemctl restart fail2ban sudo systemctl enable fail2ban
Step 6: Enable Automatic Security Updates
Keeping your server updated is essential to patch vulnerabilities.
For Ubuntu/Debian
- Install unattended upgrades:
sudo apt install unattended-upgrades -y - Enable automatic updates:
sudo dpkg-reconfigure unattended-upgrades
For CentOS/RHEL
- Install
yum-cron:sudo yum install yum-cron -y - Enable automatic updates:
sudo systemctl enable --now yum-cron
Step 7: Monitor Your VPS Performance
Monitoring your server helps you detect unusual activity.
Check CPU, Memory, and Disk Usage
Run:
htop
If not installed, install it:
sudo apt install htop -y # Ubuntu/Debian
sudo yum install htop -y # CentOS/RHEL
Check Active Network Connections
netstat -tulnp
Monitor System Logs
To check login attempts:
sudo cat /var/log/auth.log10. Monitoring and Maintaining Your Linux VPS
Now that your Linux VPS is secure, it’s essential to monitor performance, schedule backups, and optimize the server to ensure smooth operation. Regular maintenance prevents downtime, security breaches, and performance degradation.
Step 1: Set Up Automatic Backups
Backups protect your website from accidental data loss, hacking, or system failure.
Option 1: Backup with Rsync (Manual & Automated)
Rsync is a powerful command-line tool for copying and synchronizing files between directories or remote servers.
-
Create a backup folder:
sudo mkdir /backup
-
Backup website files:
sudo rsync -av --delete /var/www/html/ /backup/
-
Backup MySQL database:
mysqldump -u root -p --all-databases > /backup/db_backup.sql
-
Automate backups with a cron job:
Open the crontab file:
sudo crontab -e
Add the following lines to schedule backups daily at midnight:
0 0 * * * rsync -av --delete /var/www/html/ /backup/
0 0 * * * mysqldump -u root -pYourPassword --all-databases > /backup/db_backup.sql
Option 2: Use Cloud Storage (Google Drive, Dropbox, S3)
To back up to Google Drive or Amazon S3, install rclone:
-
Install rclone:
curl https://rclone.org/install.sh | sudo bash
-
Configure rclone:
rclone config
-
Sync backup to cloud storage:
rclone sync /backup remote:backup-folder
Step 2: Monitor System Performance
Monitoring your VPS helps detect performance bottlenecks before they cause problems.
Check System Resource Usage
-
Check CPU, memory, and running processes:
htop
If htop is not installed, install it:
sudo apt install htop -y # Ubuntu/Debian
sudo yum install htop -y # CentOS/RHEL
-
Check disk usage:
df -h
-
Check memory usage:
free -m
Step 3: Set Up Server Monitoring Tools
Use monitoring tools like Netdata or Monit for real-time tracking.
Install Netdata for Real-Time Monitoring
- Install Netdata:
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
- Access Netdata:
Open your browser and go to:
http://your-vps-ip:19999
- Netdata provides a real-time dashboard to monitor CPU, memory, bandwidth, and logs.
Step 4: Optimize Server Performance
1. Enable Caching
Caching improves website loading speed and reduces server load.
-
For Apache:
sudo a2enmod expires headers
sudo systemctl restart apache2
-
For Nginx:
Open the Nginx config file:
sudo nano /etc/nginx/nginx.conf
Add:
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=MYCACHE:10m inactive=60m;
Restart Nginx:
sudo systemctl restart nginx
2. Optimize MySQL/MariaDB Performance
Edit the MySQL configuration file:
sudo nano /etc/mysql/my.cnf # Ubuntu/Debian
sudo nano /etc/my.cnf # CentOS
Add these optimizations:
max_connections = 100
query_cache_size = 64M
innodb_buffer_pool_size = 256M
Restart MySQL:
sudo systemctl restart mysql
3. Set Up a CDN (Content Delivery Network)
A CDN like Cloudflare improves site speed by caching static content.
- Sign up at https://www.cloudflare.com.
- Add your website and change your DNS settings to use Cloudflare.
- Enable caching and DDoS protection.
Step 5: Enable Log Monitoring
1. View Server Logs
- Apache logs:
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
- Nginx logs:
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
- System logs:
sudo journalctl -xe
Step 6: Set Up Email Alerts for Server Issues
Use sendmail or Postfix to get notifications when issues occur.
1. Install Postfix
sudo apt install postfix mailutils -y # Ubuntu/Debian
sudo yum install postfix mailx -y # CentOS
2. Send a Test Email
echo "Test Email from VPS" | mail -s "VPS Test Email" your-email@example.com
Step 7: Keep Your VPS Updated
Always update software to patch security vulnerabilities.
- Update Ubuntu/Debian:
sudo apt update && sudo apt upgrade -y
- Update CentOS/RHEL:
sudo yum update -y
Step 8: Restart Your VPS When Needed
If you make major changes, restart your server:
sudo reboot
Final Thoughts
🎉 Congratulations! You have successfully set up, secured, and optimized your Linux VPS for hosting a website. 🚀
Summary of What You’ve Accomplished:
✅ Set up a Linux VPS
✅ Installed Apache/Nginx, MySQL/MariaDB, and PHP
✅ Configured a domain name and SSL (HTTPS)
✅ Deployed a website or WordPress
✅ Secured the VPS against attacks
✅ Set up monitoring and automatic backups
✅ Optimized performance and security
Now your website is fully functional, secure, and optimized. 🎯

Comments
Post a Comment