[运维笔记]Ubuntu24.04 Nginx+php配置服务器部署教程(一)

1.简介

平时日常使用网站,是由前端+后端构成这点大家众所周知,从前往后,浏览器负责渲染代码文件,让代码文件变成可以看见的图像,后端服务器则负责处理逻辑上的任务,今天所要介绍的php和nginx就是后端的一部分

Nginx

Nginx是一个高性能http和反向代理的web服务器,主要用于处理流量分配,负载均衡等任务,和tomcat功能类似相近,但功能不同,两者都可以对web页面进行转发,让用户通过http请求向服务器get到页面

PHP(Hypertext Preprocessor)

php是开源服务器端的脚本语言,他嵌入于html中,可以和数据库交互生成动态页面。

2.部署步骤

在服务器端下载Nginx

更新apt并install nginx

root@xiaoyu:/# sudo apt update
root@xiaoyu:/# sudo apt install nginx

查看nginx运行状态

root@xiaoyu:/# sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-05-12 14:55:51 UTC; 35s ago
       Docs: man:nginx(8)
    Process: 2507 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 2509 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 2510 (nginx)
      Tasks: 3 (limit: 4554)
     Memory: 2.4M (peak: 2.6M)
        CPU: 28ms
     CGroup: /system.slice/nginx.service
             ├─2510 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─2511 "nginx: worker process"
             └─2512 "nginx: worker process"

May 12 14:55:51 xiaoyu systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
May 12 14:55:51 xiaoyu systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.

运行状态良好,接下来安装php

root@xiaoyu:/# apt -y install software-properties-common apt-transport-https lsb-release ca-certificates
root@xiaoyu:/# add-apt-repository ppa:ondrej/php
root@xiaoyu:/bin# sudo apt install php7.3-fpm

查看运行状态,运行正常

root@xiaoyu:/bin# systemctl status php7.3-fpm
● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php7.3-fpm.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-05-12 15:20:05 UTC; 1min 18s ago
       Docs: man:php-fpm7.3(8)
    Process: 33951 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.3/fpm/pool.d/www.conf 73 (code=exited, status=0/SUCCESS)
   Main PID: 33948 (php-fpm7.3)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 4554)
     Memory: 6.9M (peak: 7.7M)
        CPU: 66ms
     CGroup: /system.slice/php7.3-fpm.service
             ├─33948 "php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)"
             ├─33949 "php-fpm: pool www"
             └─33950 "php-fpm: pool www"

May 12 15:20:05 xiaoyu systemd[1]: Starting php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager...
May 12 15:20:05 xiaoyu systemd[1]: Started php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager.

两者的配置文件都位于/etc/下

配置nginx

进入/etc/nginx,编辑文件nginx.conf,添加一下内容

server{
    listen 8080;
    root /var/www/html;
    index info.php;
    server_name 192.168.199.134;

        location ~ .php$ {
                        root           /var/www/html;
                        fastcgi_pass   unix:/run/php/php7.3-fpm.sock;
                        fastcgi_index  index.php;
                        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                        include        fastcgi_params;
        }
}

其中,root路径应该和我一样,index页面为主页面,server_name为主机ip,location是为了仿制nginx无法识别php后缀文件,这里先说如何配置,后期在说为什么要配置这些

重启服务

root@xiaoyu:/etc/nginx# systemctl restart nginx

配置PHP

php可以不用管,在/var/www/html,下创建文件即可

3.常见问题

浏览器访问php页面时直接下载,而非渲染进浏览器,

这种情况多半是你的location没有配置好,重新复制一下location板块粘贴进去,可能能解决

安装php时报错Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xeu apache2.service" for details.

这个报错其实就是因为nginx把Apache的端口给占用了,如果想要启用Apache换个端口号或者将nginx停用即可

以上就是配置ip的过程和报错的解决方法,之后如果有补充我会尽快发出

4.最后声明

文章主要是自己的一份笔记,如果能提供帮助本人会非常开心,如果有什么问题也欢迎提出来,会尽快整改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值