You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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:

1. Bind Your Domain to the VPS

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) or www for 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 with nslookup yourdomain.com later.
  • 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/hosts
    

    Add this line at the bottom:

    [Your VPS Public IP] yourdomain.com www.yourdomain.com
    

    Save and exit (in vi, press Esc, type :wq, then hit Enter).

2. Install a Web Server Stack (LAMP for PHP)

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 -y
    
  • Install Apache Web Server

    sudo yum install httpd -y
    

    Start Apache and set it to run on boot:

    sudo systemctl start httpd
    sudo systemctl enable httpd
    

    Open port 80 (HTTP) in the firewall:

    sudo firewall-cmd --permanent --add=http
    sudo firewall-cmd --reload
    

    Test 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 -y
    

    Start and enable MariaDB:

    sudo systemctl start mariadb
    sudo systemctl enable mariadb
    

    Run the security script to set a root password and secure the database:

    sudo mysql_secure_installation
    

    Follow the prompts—answer Y to 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 -y
    

    Restart Apache to apply PHP changes:

    sudo systemctl restart httpd
    

    Verify PHP works by creating a test file:

    sudo vi /var/www/html/info.php
    

    Paste 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.

3. Website File Upload Path

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
    Replace yourdomain.com with your actual domain:

    sudo mkdir -p /var/www/yourdomain.com/public_html
    

    Set 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.com
    
  • Configure a Virtual Host
    Create a new Apache config file for your domain:

    sudo vi /etc/httpd/conf.d/yourdomain.com.conf
    

    Paste 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 httpd
    

    Now you can upload all your website files (HTML, PHP, images, etc.) to /var/www/yourdomain.com/public_html/ using SFTP or SCP.

4. SSL Certificate Upload & Configuration

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 -y
    

    Install Certbot and the Apache plugin:

    sudo yum install certbot python2-certbot-apache -y
    
  • Obtain & Install SSL Certificate
    Run this command and follow the interactive prompts:

    sudo certbot --apache
    

    You'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 chain
    • privkey.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 -e
    

    Add this line to run the renewal check daily at noon:

    0 12 * * * /usr/bin/certbot renew --quiet
    

    Save and exit—this will automatically renew certificates when they're close to expiring.


内容的提问来源于stack exchange,提问作者Mohammad Amin Ghaseminia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:33:00