Ubuntu 24.04 安排 nginx + php-fpm,入门PHP必须!
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/f55a347cd7744a44828054a9ce534e56~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728207822&x-signature=8HIobYLND9pGtPtIxle3I8LlSGU%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Nginx + PHP-FPM</p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">nginx 是一个流行的 web 服务器,以其速度和<span style="color: black;">靠谱</span>性而闻名,被许多顶级网站<span style="color: black;">运用</span>。要用 PHP 运行网站,您需要设置 php-fpm,nginx 与 php-fpm <span style="color: black;">一块</span>处理 PHP 文件并将其<span style="color: black;">表示</span>给用户,<span style="color: black;">帮忙</span>网站更快地加载并处理<span style="color: black;">更加多</span>流量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本教程将<span style="color: black;">帮忙</span>您在 ubuntu 24.04 系统上安装和配置 NGINX 和 PHP-FPM,创建虚拟主机并<span style="color: black;">运用</span> Let s Encrypt SSL 启用 HTTPS 来<span style="color: black;">守护</span>您的网站。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Step 1: Update Your System</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>需要<span style="color: black;">保证</span>你的系统是最新的。</p><span style="color: black;">sudo</span> <span style="color: black;">apt update</span>
<span style="color: black;">sudo</span> <span style="color: black;">apt upgrade -y</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Step 2: Install NGINX Server</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>,让<span style="color: black;">咱们</span>安装 NGINX web 服务器,遵循以下<span style="color: black;">过程</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1) 安装 nginx</p><span style="color: black;">sudo</span> apt install nginx
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2) <span style="color: black;">起步</span> nginx 服务</p><span style="color: black;">sudo</span> systemctl start nginx
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3) 设置 nginx 开机<span style="color: black;">起步</span></p>sudo systemctl <span style="color: black;">enable</span> nginx
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4) <span style="color: black;">检测</span> nginx 服务状态</p>sudo systemctl <span style="color: black;">status</span> nginx
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Step 3: Install PHP-FPM</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">根据</span>以下<span style="color: black;">过程</span>安装 PHP-FPM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1) 安装 php-fpm</p><span style="color: black;">sudo</span> apt install php-fpm
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2) <span style="color: black;">起步</span> php-fpm 服务</p><span style="color: black;">sudo</span> systemctl start php-fpm
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3) 设置 php-fpm 开机<span style="color: black;">起步</span></p>sudo systemctl <span style="color: black;">enable</span> php-fpm
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4) <span style="color: black;">检测</span> php-fpm 服务状态</p>sudo systemctl <span style="color: black;">status</span> php-fpm
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Step 4: Configure NGINX to Use PHP-FPM</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">让<span style="color: black;">咱们</span>为您的网站创建一个新的主机文件并将其配置为<span style="color: black;">运用</span> PHP-FPM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1) 切换到 NGINX sites-available 目录</p><span style="color: black;">cd</span> /etc/nginx/sites-available/
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2) 为网站创建一个新的配置文件,将“example.com”替换为您的<span style="color: black;">实质</span>域名。</p><span style="color: black;">sudo</span> nano /etc/nginx/sites-available/example.com
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3) 将以下配置添加到文件中,<span style="color: black;">保证</span>将“example.com”替换为您的<span style="color: black;">实质</span>域名。</p><span style="color: black;">server</span> {
<span style="color: black;">listen</span> <span style="color: black;">80</span>;
<span style="color: black;">server_name</span>example.com www.example.com;<span style="color: black;">root</span> /var/www/html/example.com;
<span style="color: black;">index</span> index.php index.html index.htm;
<span style="color: black;">location</span> / {
<span style="color: black;">try_files</span> <span style="color: black;">$uri</span> <span style="color: black;">$uri</span>/ =<span style="color: black;">404</span>;
}
<span style="color: black;">location</span> <span style="color: black;">~ \.php$</span> {
<span style="color: black;">include</span>snippets/fastcgi-php.conf;<span style="color: black;">fastcgi_pass</span> unix:/var/run/php/php7.4-fpm.sock;
}
<span style="color: black;">location</span> <span style="color: black;">~ /\.ht</span> {
<span style="color: black;">deny</span> all;
}
}
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">保留</span>并退出配置文件</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为您的网站创建文档根目录</p>sudo mkdir -p /<span style="color: black;">var</span>/www/html/example.com<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将目录的所有权分配给当前用户</p><span style="color: black;">sudo</span> chown -R <span style="color: black;">$USER</span>:<span style="color: black;">$USER</span> /var/www/html/example.com
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置目录权限</p>sudo chmod -R <span style="color: black;">755</span> /<span style="color: black;">var</span>/www/html/example.com
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建一个简单的 PHP 文件来测试您的配置</p>nano /<span style="color: black;">var</span>/www/html/example.com/index.php<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将以下行添加到 index.php 文件中,<span style="color: black;">保留</span>并退出。</p><span style="color: black;"><span style="color: black;"><?php</span> phpinfo(); <span style="color: black;">?></span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">把网站 example.com 的配置文件软连接到 sites-enabled 目录</p>sudo ln -s <span style="color: black;">/etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">测试 NGINX 配置<span style="color: black;">是不是</span>有语法错误</p><span style="color: black;">sudo</span> nginx -t
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>测试成功,重新加载 NGINX 以应用更改</p><span style="color: black;">sudo</span> systemctl reload nginx
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">打开您的 Web 浏览器,<span style="color: black;">而后</span><span style="color: black;">拜访</span> <strong style="color: blue;">http//example.com</strong> 您应该<span style="color: black;">能够</span>看到“ PHP Info”页面,这<span style="color: black;">寓意</span>着您的 NGINX 服务器已正确配置为<span style="color: black;">运用</span> PHP-FPM。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Step 5: Secure Your Website with Let’s Encrypt SSL</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要<span style="color: black;">运用</span> HTTPS <span style="color: black;">守护</span>您的网站,您<span style="color: black;">能够</span><span style="color: black;">运用</span> Let s Encrypt SSL,请遵循以下<span style="color: black;">过程</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1) 安装 Certbot</p><span style="color: black;">sudo</span> apt install certbot python3-certbot-nginx
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2) 运行 Certbot 获取并安装 SSL 证书,<span style="color: black;">根据</span>提示完成安装。</p>sudo certbot <span style="color: black;">--nginx</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3) <span style="color: black;">经过</span>运行演练来验证 Certbot 自动更新</p>sudo certbot renew<span style="color: black;">--dry-run</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">我的开源项目</h1>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/1c6dd8524d044606b05ce51cb0f67525~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728207822&x-signature=Q7rQ0SXn%2FU9PUUIpZDKfCIv%2BJCM%3D" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">酷瓜云课堂 - 开源在线教育<span style="color: black;">处理</span><span style="color: black;">方法</span></p>
</div>course-tencent-cloud(酷瓜云课堂 - gitee 仓库)course-tencent-cloud(酷瓜云课堂 - github 仓库)
认真阅读了楼主的帖子,非常有益。 “NB”(牛×的缩写,表示叹为观止) 期待楼主的下一次分享!”
页:
[1]