如何在CentOS 7 VPS部署PHP网站(不使用控制面板)
Hey there, let's walk through each step you need to get your PHP site up and running on a CentOS 7 VPS without any control panels like cPanel. I'll break everything down clearly so you can follow along easily:
This has two key parts: updating your domain's DNS records and configuring the VPS to recognize the domain.
Step 1: Configure DNS Resolution
Log into your domain registrar's dashboard, find the DNS management section, and add an A record:- Host record: Use
@for your root domain (e.g.,yourdomain.com) orwwwfor the www subdomain - Value: Paste your VPS's public IP address
- TTL: Leave it at the default (usually 300 seconds)
Wait 5-10 minutes for DNS changes to propagate—you can verify withnslookup yourdomain.comlater.
- Host record: Use
Step 2: Local Domain Mapping (Optional but Recommended)
Edit the VPS's hosts file to map your domain to its local IP, which helps with testing:sudo vi /etc/hostsAdd this line at the bottom:
[Your VPS Public IP] yourdomain.com www.yourdomain.comSave and exit (in vi, press
Esc, type:wq, then hit Enter).
Since you're deploying a PHP site, the LAMP stack (Linux + Apache + MariaDB + PHP) is the standard choice. Here's how to set it up:
Update System Packages First
sudo yum update -yInstall Apache Web Server
sudo yum install httpd -yStart Apache and set it to run on boot:
sudo systemctl start httpd sudo systemctl enable httpdOpen port 80 (HTTP) in the firewall:
sudo firewall-cmd --permanent --add=http sudo firewall-cmd --reloadTest Apache by visiting your VPS's public IP in a browser—you should see the default CentOS Apache page.
Install MariaDB (MySQL Alternative)
CentOS 7 uses MariaDB as the default MySQL-compatible database:sudo yum install mariadb-server mariadb -yStart and enable MariaDB:
sudo systemctl start mariadb sudo systemctl enable mariadbRun the security script to set a root password and secure the database:
sudo mysql_secure_installationFollow the prompts—answer
Yto all security questions for best practice.Install PHP and Required Extensions
Install PHP and common extensions needed for most PHP sites:sudo yum install php php-mysql php-gd php-xml php-mbstring -yRestart Apache to apply PHP changes:
sudo systemctl restart httpdVerify PHP works by creating a test file:
sudo vi /var/www/html/info.phpPaste this content:
<?php phpinfo(); ?>Save and exit, then visit
http://yourdomain.com/info.php—you should see a PHP info page if everything's set up correctly.
By default, Apache serves files from /var/www/html/, but for better organization (especially if you plan to host multiple sites), use a virtual host setup:
Create a Custom Root Directory
Replaceyourdomain.comwith your actual domain:sudo mkdir -p /var/www/yourdomain.com/public_htmlSet proper permissions so Apache can access the files:
sudo chown -R apache:apache /var/www/yourdomain.com/public_html sudo chmod -R 755 /var/www/yourdomain.comConfigure a Virtual Host
Create a new Apache config file for your domain:sudo vi /etc/httpd/conf.d/yourdomain.com.confPaste this configuration (replace placeholders with your details):
<VirtualHost *:80> ServerAdmin your-email@example.com ServerName yourdomain.com ServerAlias www.yourdomain.com DocumentRoot /var/www/yourdomain.com/public_html ErrorLog /var/log/httpd/yourdomain.com-error.log CustomLog /var/log/httpd/yourdomain.com-access.log combined </VirtualHost>Save and exit, then restart Apache:
sudo systemctl restart httpdNow you can upload all your website files (HTML, PHP, images, etc.) to
/var/www/yourdomain.com/public_html/using SFTP or SCP.
Let's use Let's Encrypt's free SSL certificates (the easiest and most popular option):
Install Certbot (SSL Tool)
First, enable the EPEL repository (required for Certbot):sudo yum install epel-release -yInstall Certbot and the Apache plugin:
sudo yum install certbot python2-certbot-apache -yObtain & Install SSL Certificate
Run this command and follow the interactive prompts:sudo certbot --apacheYou'll need to enter your email, agree to the terms, and select which domain to secure. Certbot will automatically configure Apache to use SSL and redirect HTTP traffic to HTTPS.
Certificate Storage Location
Certbot stores your SSL files in/etc/letsencrypt/live/yourdomain.com/:fullchain.pem: The full SSL certificate chainprivkey.pem: Your private key
You don't need to manually move these files—Certbot updates Apache's virtual host config to reference them directly.
Set Up Auto-Renewal
Let's Encrypt certificates expire after 90 days, so set up automatic renewal:sudo crontab -eAdd this line to run the renewal check daily at noon:
0 12 * * * /usr/bin/certbot renew --quietSave and exit—this will automatically renew certificates when they're close to expiring.
内容的提问来源于stack exchange,提问作者Mohammad Amin Ghaseminia

