How to Host a PHP Website with MySQLi on a VPS
1. Get a VPS
Choose a VPS provider such as DigitalOcean, AWS EC2, Linode, Vultr, or Hostinger. Follow their instructions to create and configure your VPS.
2. Access Your VPS
Use SSH to access your VPS using the command:
ssh root@your_vps_ip
3. Update Your VPS
Before installing software, update your VPS:
apt update && apt upgrade -y
yum update -y
4. Install Apache Web Server
Install Apache to serve your website:
apt install apache2 -y
yum install httpd -y
5. Install PHP
Install PHP and necessary modules:
apt install php php-mysqli php-cli php-fpm php-mbstring php-curl php-xml -y
yum install php php-mysqli php-cli php-fpm php-mbstring php-curl php-xml -y
6. Install MySQL Database
Install MySQL or MariaDB for database management:
apt install mysql-server -y
yum install mysql-server -y
7. Create a MySQL Database and User
Log in to MySQL and create a database and user:
CREATE DATABASE my_database;
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON my_database.* TO 'my_user'@'localhost';
8. Upload Your Website Files
You can use FTP or SCP to upload your website files to the server. Place them in the /var/www/html directory.
9. Set Proper Permissions
Set the correct permissions for the Apache server to access your files:
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
10. Test Your Website
Visit your server's IP or domain to test your website:
http://your_vps_ip
11. Configure Virtual Host (Optional)
If you’re hosting multiple sites, set up a virtual host:
nano /etc/apache2/sites-available/mydomain.com.conf
nano /etc/httpd/conf.d/mydomain.com.conf
12. Setup SSL (Optional)
Use Let’s Encrypt to install SSL certificates:
certbot --apache -d mydomain.com -d www.mydomain.com
13. Configure PHP and MySQLi for Your Website
Ensure your PHP code uses MySQLi to connect to the database:
<?php $servername = "localhost"; $username = "my_user"; $password = "my_password"; $dbname = "my_database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Your code to fetch or insert data goes here $conn->close(); ?>
14. Troubleshooting
Check Apache and MySQL logs for errors:
tail -f /var/log/apache2/error.log
tail -f /var/log/httpd/error_log
Frequently Asked Questions
What are the requirements to host a PHP website?
To host a PHP website, you need:
-
A web hosting provider that supports PHP (e.g., Bluehost, Hostinger, SiteGround)
-
A domain name
-
A MySQL or MariaDB database (if required by your application)
-
An FTP client or control panel to upload files
How can I upload my PHP website to a hosting server?
You can upload your PHP website using:
-
FTP (File Transfer Protocol): Use an FTP client like FileZilla to transfer files.
-
cPanel File Manager: Upload files directly through your hosting provider’s control panel.
-
SSH (Secure Shell): If your hosting supports it, use SSH for command-line file transfers.
Do I need to configure a database when hosting a PHP website?
If your website uses a database (e.g., MySQL), you need to:
-
Create a new database using cPanel or phpMyAdmin.
-
Update the database credentials (host, username, password, database name) in your
config.php
or.env
file. -
Import your SQL database using phpMyAdmin or a command-line tool.
How do I make my PHP website live on a domain?
To connect your PHP website to a domain:
-
Purchase a domain name from a registrar like GoDaddy or Namecheap.
-
Update the domain's nameservers to match your hosting provider’s settings.
-
Upload your website files to the
public_html
or root directory of your hosting. -
Configure DNS settings to ensure proper redirection.
How can I secure my hosted PHP website?
To keep your PHP website secure:
-
Install an SSL certificate (HTTPS) to encrypt data.
-
Keep PHP and CMS (WordPress, Laravel, etc.) updated.
-
Use secure database connections (prepared statements or PDO).
-
Set proper file permissions to prevent unauthorized access.
-
Use a web application firewall (WAF) for added protection.