在Ubuntu上部署PHP网站可以通过多种方式实现,以下是一个基本的步骤指南,使用Apache作为Web服务器和PHP-FPM(FastCGI Process Manager)来处理PHP请求。
首先,更新你的Ubuntu系统以确保所有软件包都是最新的。
sudo apt update
sudo apt upgrade -y
安装Apache Web服务器及其相关模块。
sudo apt install apache2 -y
启动Apache服务并设置它在系统启动时自动运行。
sudo systemctl start apache2
sudo systemctl enable apache2
安装PHP以及处理PHP请求所需的模块。
sudo apt install php libapache2-mod-php php-mysql -y
安装PHP后,重启Apache服务以加载新的PHP模块。
sudo systemctl restart apache2
创建一个新的虚拟主机配置文件来托管你的PHP网站。
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
在文件中添加以下内容:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com
<Directory /var/www/yourdomain.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
保存并关闭文件。
启用刚刚创建的虚拟主机配置。
sudo a2ensite yourdomain.com.conf
禁用默认的Apache站点配置。
sudo a2dissite 000-default.conf
再次重启Apache服务以应用更改。
sudo systemctl restart apache2
确保你的防火墙允许HTTP和HTTPS流量。
sudo ufw allow 'Apache Full'
你可以使用Let’s Encrypt来安装免费的SSL证书。
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
按照提示完成SSL证书的安装和配置。
将你的PHP网站文件上传到/var/www/yourdomain.com
目录下。
打开浏览器并访问你的域名,确保网站正常运行。
通过以上步骤,你可以在Ubuntu上成功部署一个PHP网站。根据你的具体需求,你可能还需要进行一些额外的配置,例如数据库设置、缓存优化等。