ubuntu安装nginx,php7.4
时间: 2025-07-11 09:02:12 浏览: 11
在Ubuntu系统上安装Nginx和PHP 7.4,可以按照以下步骤进行操作:
### 安装 Nginx
1. 更新软件包列表:
```bash
sudo apt update
```
2. 安装 Nginx:
```bash
sudo apt install nginx
```
3. 检查 Nginx 是否成功启动:
```bash
systemctl status nginx
```
### 安装 PHP 7.4 及其依赖
1. 添加 `ondrej/php` 的 PPA 仓库以获取 PHP 7.4 支持:
```bash
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
```
2. 安装 PHP 7.4 及常用扩展(根据需要调整):
```bash
sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-cli php7.4-opcache
```
3. 确保安装了 SQLite3 和 Oniguruma 库(如引用中提到的问题):
```bash
sudo apt install sqlite3 libsqlite3-dev
git clone https://github.com/kkos/oniguruma.git
cd oniguruma
./autogen.sh
./configure
make
sudo make install
```
### 配置 Nginx 与 PHP-FPM 集成
1. 编辑默认的 Nginx 站点配置文件 `/etc/nginx/sites-available/default`,确保包含以下内容:
```nginx
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
```
2. 测试并重新加载 Nginx 配置:
```bash
sudo nginx -t
sudo systemctl reload nginx
```
3. 启动并启用 PHP-FPM 服务:
```bash
sudo systemctl enable php7.4-fpm
sudo systemctl start php7.4-fpm
```
### 创建测试页面验证安装
1. 创建一个简单的 PHP 测试文件:
```bash
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
```
2. 打开浏览器访问 `http://your-server-ip/info.php`,如果看到 PHP 信息页,则说明安装成功。
###
阅读全文
相关推荐



















