暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

超详细!CentOS 7.4 64位 搭建LNMP

蒲东平 2023-12-06
214
1、准备工作
一般来说,云服务器或者 vps 安装的系统镜像都不是最新的,所以我们连接上服务器之后,必须尽快更新服务器的系统以及软件,这样可以更好的保障我们的服务器系统安全
  • 服务器系统以及软件升级命令

yum -y update

CentOS 系列的服务器系统有一个毛病,就是官方自带的源的软件比较古老,并且很多的软件都没有。因为他们的首要任务是保证服务器的稳定,而不是追求最新。但是太过于保守了,一般来说,我们会给服务器添加一个 epel-release 这个源。这个源里包含了例如 nginx 之类的我们需要的软件,使用起来比较方便。

  • 安装 epel-release 源

yum install epel-release -y

通过上面的命令进行安装。确认是否安装成功,可以用下面的命令检测一下

yum search nginx

如果搜索的结果包含下面的这行内容,就表示安装成功了,然后我们就能愉快的安装我们需要的软件了

nginx.x86_64 : A high performance web server and reverse proxy server

我昨天在配置的时候发现不能搜索出来,但是确实是安装上了。后来检查了一下 etc/yum.repos.d/epel.repo 文件,发现里面配置不对,修改了一下就好。主要是 epel 段落中的 enabled 值默认设置为 0 了,我们将值改成 1 就可以了

2、安装 nginx

接着是安装 Nginx ,虽然安装 EPEL 之后,源里就自带了 Nginx ,但是,出于各个平台的统一以及版本的考虑,我更倾向安装 Nginx 官方源里的,新建一个 repo 文件,然后把下列内容黏贴进去:

  • 新建 nginx.repo 文件

vim /etc/yum.repos.d/nginx.repo

  • 将以下内容添加进 nginx.repo 文件中去

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

这样就相当于添加了 Nginx 的官方源,官方相关页面在这里 https://nginx.org/en/linux_packages.html#stable

  • 安装 nginx

yum install nginx -y

  • 启动 nginx

systemctl start nginx

  • 将 nginx 设置为开机启动

systemctl enable nginx

  • 检测 nginx 是否已经安装成功

// 方法01、curl 127.0.0.1 => 可以看到 html 源代码
// 方法02、直接使用浏览器访问服务器 IP 可以看到 nginx 默认首页

3、安装 mysql 和 php 和本目录下 搭建 LAMP 中教程一样

4、配置 nginx

通过源安装的 Nginx,默认配置文件目录在 etc/nginx/,主配置文件是 etc/nginx/nginx.conf,我们就从这个文件开始配置

这是nginx.conf默认内容:


user nginx;
worker_processes 1;

error_log var/log/nginx/error.log warn;
pid var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include etc/nginx/conf.d/*.conf;
}


5、配置 php

因为我们是利用 Nginx 搭配 PHP-FPM 工作,所以,还需要对 PHP 及 PHP-FPM 进行相应配置。PHP 相关配置文件都在 etc 或者 etc/php* 目录下,我们需要对这些文件进行修改。

首先更改 etc/php.ini ,里面有一行 cgi.fix_pathinfo=1
,我们需要将其找到,并将其值改成 0,大概在 762 行,找到,将其修改:

vim etc/php.ini

762 cgi.fix_pathinfo=0

修改 PHP-FPM 配置   => vim etc/php-fpm.d/www.conf
将第8行 user = apache
修改成 user = nginx

将第10行 group = apache
修改成 group = nginx

  1 ; Start a new pool named 'www'.
2 [www]
3
4 ; Unix user/group of processes
5 ; Note: The user is mandatory. If the group is not set, the default user's group
6 ; will be used.
7 ; RPM: apache Choosed to be able to access some dir as httpd
8 user = nginx
9 ; RPM: Keep a group allowed to write in log dir.
10 group = nginx
11
12 ; The address on which to accept FastCGI requests.
13 ; Valid syntaxes are:
14 ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
15 ; a specific port;
16 ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
17 ; a specific port;
18 ; 'port' - to listen on a TCP socket to all addresses
19 ; (IPv6 and IPv4-mapped) on a specific port;
20 ; '/path/to/unix/socket' - to listen on a unix socket.
21 ; Note: This value is mandatory.
22 listen = 127.0.0.1:9000

6、添加网站

vim etc/nginx/conf.d/www.drling.xin.conf

server {
listen 80;
server_name www.drling.xin;


location {
root var/www/www.drling.xin/public;
index index.php index.html index.htm;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
root /var/www/www.drling.xin/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}



最后说个事
公号算法变了,为防止看不到我的更新
大家帮忙加个星标
点击上方的公众号卡片
再点右上角三个点
就能看到设为星标
算我跪下来求你们

往期精选:

还在使用默认的微信图标?赶紧换个吧!

我的微信和你们的不一样!?

微信又出了款重磅APP,体验下?

文章转载自蒲东平,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论