Linux下CentOS 7手动部署LNMP环境

本文详细介绍了在Linux的CentOS 7系统中如何手动部署LNMP环境,包括Nginx、MySQL和PHP的安装、配置及防火墙设置。从关闭防火墙和SELinux,到安装依赖,再到下载和安装各组件,最后测试通过访问phpinfo.php确认部署成功。
摘要由CSDN通过智能技术生成

一、准备编译环境

1.远程连接Linux实例

ssh root@192.168.0.197

2.关闭防火墙

i. 运行systemctl status firewalld命令查看当前防火墙的状态

如果防火墙的状态参数是inactive,则防火墙为关闭状态

如果防火墙的状态参数是active,则防火墙为开启状态。本示例中防火墙为开启状态,因此需要关闭防火墙

ii.关闭防火墙。如果防火墙为关闭状态可以忽略此步骤

如果您想临时关闭防火墙,运行命令systemctl stop firewalld说明这只是暂时关闭防火墙,下次重启Linux后,防火墙还会开启。

如果您想永久关闭防火墙,运行命令systemctl disable firewalld (说明 如果您想重新开启防火墙,请参见firewalld官网信息)

[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since 六 2021-06-05 15:48:13 CST; 2min 13s ago
     Docs: man:firewalld(1)
 Main PID: 749 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─749 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

605 15:48:11 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
605 15:48:13 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

3.关闭SELinux

i.运行getenforce命令查看SELinux的当前状态

如果SELinux状态参数是Disabled,则SELinux为关闭状态

如果SELinux状态参数是Enforcing,则SELinux为开启状态。本示例中SELinux为开启状态,因此需要关闭SELinux

ii.关闭SELinux。如果SELinux为关闭状态可以忽略此步骤

如果您想临时关闭SELinux,运行命令setenforce 0 (说明这只是暂时关闭SELinux,下次重启Linux后,SELinux还会开启)

如果您想永久关闭SELinux,运行命令 vi /etc/selinux/config 编辑SELinux配置文件。回车后,把光标移动到 SELINUX=enforcing 这一行,按 i 键进入编辑模式,修改为 SELINUX=disabled ,按 Esc 键,然后输入 :wq 并按 Enter 键以保存并关闭SELinux配置文件(说明 如果您想重新开启SELinux,请参见开启或关闭SELinux。)

iii.重启系统使设置生效

[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# vi /etc/selinux/config

[root@localhost ~]# reboot
Connection to 192.168.0.193 closed by remote host.
Connection to 192.168.0.193 closed.

C:\Users\beauty>ssh root@192.168.0.193
root@192.168.0.193's password:
Last login: Wed Jun  2 19:39:09 2021 from 192.168.0.197
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# reboot
Connection to 192.168.0.197 closed by remote host.
Connection to 192.168.0.197 closed.

C:\Users\beauty>ssh root@192.168.0.197
root@192.168.0.197's password:
Last login: Sat Jun  5 15:50:02 2021 from 192.168.0.194
[root@localhost ~]# getenforce
Disabled

4.Centos7下安装netstat和lsof

i. 安装netstat

[root@localhost ~]# yum install net-tools
#查看端口
[root@localhost ~]# netstat -an | grep 80

ii. 安装lsof

[root@localhost ~]# yum install lsof
#查看端口进程情况
[root@localhost system]# lsof -i:80
#杀死进程
[root@localhost system]# kill -9 $(lsof -i:80 -t)

5.下载wget安装

[root@localhost ~]# yum -y install wget

6.软件版本

  • Nginx版本:Nginx 1.21.0
  • MySQL版本:MySQL 8.0.25
  • PHP版本:PHP 8.0.0

二、安装Nginx

1.安装依赖包

#一键安装上面四个依赖
[root@localhost ~]# yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

2.下载并解压安装包

#切换目录
[root@localhost ~]# cd /
#创建一个文件夹
[root@localhost ~]# mkdir -p  www/server/nginx   
#切换目录
[root@localhost ~]# cd /www/server/nginx
#下载
[root@localhost nginx]# wget http://nginx.org/download/nginx-1.21.0.tar.gz
#解压
[root@localhost nginx]# tar -xvf nginx-1.21.0.tar.gz   

3.安装nginx

#进入nginx目录
[root@localhost nginx]# cd /www/server/nginx
#进入目录
[root@localhost nginx]# cd nginx-1.21.0
#执行命令
[root@localhost nginx-1.21.0]# ./configure --prefix=/usr/local/nginx
#执行make命令
[root@localhost nginx-1.21.0]# make
#执行make install命令
[root@localhost nginx-1.21.0]# make install

4.配置nginx.conf

#备份Nginx配置文件
[root@localhost conf]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
#打开配置文件
[root@localhost conf]# vi /usr/local/nginx/conf/nginx.conf

将端口号改成8089,因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。

localhost修改为你服务器ip地址,也可以不修改。

在这里插入图片描述

5.防火墙开启

i. 开启端口8089

[root@localhost conf]# firewall-cmd --zone=public --add-port=8089/tcp --permanent

ii. 重启防火墙

[root@localhost conf]# firewall-cmd --reload

iii. 查看已经开放的端口

[root@localhost conf]# firewall-cmd --list-ports

6.运行以下命令,进入Nginx的sbin目录,然后启动Nginx

[root@localhost nginx]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx  

i. 安装完成一般常用命令

cd /usr/local/nginx/sbin      首先进入 sbin 目录
./nginx              启动 Nginx
./nginx -s stop      停止 Nginx
./nginx -s reload    重新加载 Nginx
./nginx -v           查看 Nginx 版本

7.查看nginx是否成功启动了

[root@localhost sbin]# ps -ef | grep nginx
root       4338      1  0 16:48 ?        00:00:00 nginx: master process ./nginx
nobody     4339   4338  0 16:48 ?        00:00:00 nginx: worker process
root       4341   1549  0 16:48 pts/0    00:00:00 grep --color=auto nginx

8.配置文件修改后,需要指定配置文件进行重启

#定配置文件进行重启
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf
#检测文件是否配置正确
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

9.创建一个软链接启动nginx

#建立软链接
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
#启动
[root@localhost sbin]# nginx
#查看nginx版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.21.0

10.访问服务器ip+端口查看

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值