温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CentOS7中LNMP环境怎么搭建

发布时间:2022-04-14 14:23:28 来源:亿速云 阅读:135 作者:iii 栏目:编程语言

本文小编为大家详细介绍“CentOS7中LNMP环境怎么搭建”,内容详细,步骤清晰,细节处理妥当,希望这篇“CentOS7中LNMP环境怎么搭建”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

进入命令行后,先使用su命令切换到root权限。

首先配置防火墙

centos 7.0默认使用的是firewall作为防火墙

1.关闭firewall:

systemctl stop firewalld.service #停止firewall 
systemctl disable firewalld.service #禁止firewall开机启动

2.关闭selinux:

vi /etc/selinux/config

#selinux=enforcing #注释掉

selinux=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

CentOS7中LNMP环境怎么搭建

3.安装priorities与wget

yum install yum-priorities -y 
yum -y install wget

CentOS7中LNMP环境怎么搭建

1.安装mysql

下载mysql源安装包

复制代码 代码如下:

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

安装mysql源

复制代码 代码如下:

yum localinstall mysql57-community-release-el7-8.noarch.rpm

检查mysql源是否安装成功,注意命令里的点号。

复制代码 代码如下:

yum repolist enabled | grep "mysql.-community."

安装mysql

复制代码 代码如下:

yum install mysql-community-server

启动mysql服务,启动服务时可能会慢一些,因电脑配置各异。

复制代码 代码如下:

systemctl start mysqld

查看mysql的启动状态

复制代码 代码如下:

systemctl status mysqld

CentOS7中LNMP环境怎么搭建

开机启动

systemctl enable mysqld 
systemctl daemon-reload

查看root本地登录密码(这条命令会查出mysql设置的默认随机密码,如下图,我的随机密码为t3e4woyyi=:y)

grep 'temporary password' /var/log/mysqld.log

CentOS7中LNMP环境怎么搭建

通过随机密码登陆mysql(随机密码比较难辨认,多几次,我在登陆的时候就因为看错密码试了两次才成功)

mysql -u root -p

CentOS7中LNMP环境怎么搭建

修改mysql登陆密码(注意不要漏掉分号,这是mysql的语句,修改完成后使用exit退出后再次登陆)

set password for 'root'@'localhost'="chen123456.";
exit;

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示error 1819 (hy000): your password does not satisfy the current policy requirements错误,如下所示:

alter user ‘root'@'localhost' identified by ‘mynewpass4!';
set password for ‘root'@'localhost'=password(‘mynewpass4!');
通过msyql环境变量可以查看密码策略的相关信息:

mysql> show variables like ‘%password%';

CentOS7中LNMP环境怎么搭建

如果上面的方式不能修改可以使用下面安全模式修改root:
关闭服务
systemctl stop mysqld.service
vi /etc/my.cnf
mysqld下面添加skip-grant-tables 保存退出启动服务
systemctl start mysqld.service
mysql -u root 不用密码直接回车
use mysql
update user set authentication_string=password(‘root-123') where user='root'and host='localhost';
flush privileges;
exit;
vi /etc/my.cnf 把 skip-grant-tables 一句删除保存退出重启mysql服务
systemctl restart mysqld.service
再次登录即可
mysql -u root -proot-123

如果进行操作出现下面的提示:
you must reset your password using alter user statement before executing thisstatement.
就再设置一遍密码
set password = password(‘root-123');

开放3306端口(允许使用用户名root密码root-123456从任何主机连接到mysql服务器

mysql>grant all on root.* to root@'%' identified by 'vmroot!@#456vmroot';
mysql>flush privileges;
mysql>exit;

开启防火墙mysql 3306端口的外部访问

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd--reload

配置默认编码为utf8

vi /etc/my.cnf

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

[mysqld]
character_set_server=utf8
init_connect='set names utf8'

默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

CentOS7中LNMP环境怎么搭建

如果想使用防火墙,建议使用以下方法配置:

关闭firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

安装iptables防火墙:

yum install iptables-services #安装
sudo vi /etc/sysconfig/iptables #编辑防火墙配置文件

配置文件更改如下:

# firewall configuration written by system-config-firewall
# manual customization of this file is not recommended.
*filter
:input accept [0:0]
:forward accept [0:0]
:output accept [0:0]
-a input -m state --state established,related -j accept
-a input -p icmp -j accept
-a input -i lo -j accept
-a input -m state --state new -m tcp -p tcp --dport 22 -j accept
 
//下面是编辑添加的部分
-a input -m state --state new -m tcp -p tcp --dport 80 -j accept
-a input -m state --state new -m tcp -p tcp --dport 3306 -j accept
//以上是编辑添加的部分
 
-a input -j reject --reject-with icmp-host-prohibited
-a forward -j reject --reject-with icmp-host-prohibited
commit

然后输入:wq保存退出,在命令窗口输入以下命令使其生效:

systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动

2、关闭selinux

命令行输入以下内容,打开selinux配置文件:

sudo vi /etc/selinux/config

修改内容如下

#selinux=enforcing #注释掉 
#selinuxtype=targeted #注释掉 
selinux=disabled #增加

输入:wq!#保存退出,然后命令行输入以下内容,使其生效

setenforce 0 #使配置立即生效

2.安装php

yum默认安装的php版本较低,这次,我们准备安装php5.6版本,所以需要先安装epel库,然后安装php。

yum install epel-release

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-fpm php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-phpunit php-pecl-xdebug php-pecl-xhprof

安装完成后键入php -v会显示出php的版本,代表我们php安装完成了。

php -v

CentOS7中LNMP环境怎么搭建

3.安装nginx

复制代码 代码如下:

wget http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

然后启动nginx

systemctl start nginx.service #启动nginx 
systemctl stop nginx.service #停止 
systemctl restart nginx.service #重启 
systemctl enable nginx.service #设置开机启动

.更改nginx端口号(根据自己需求)

cd /etc/nginx/conf.d/ 
vim default.conf 
把listen 80改成listen 81
然后重启nginx
systemctl restart nginx.service #重启nginx

CentOS7中LNMP环境怎么搭建

这时我们打开浏览器,访问localhost如果出现welcome to nginx!那么nginx就安装成功了

CentOS7中LNMP环境怎么搭建

nginx安装完成了,那么该配置php-fpm了。让nginx与php联动起来。

打开php-fpm配置文件

sudo vi /etc/php-fpm.d/www.conf

修改以下内容(这里查找配置项时,可以使用斜杠加要查找的关键字回车查找,如下图所示)

listen.owner = nginx
listen.group = nginx
listen.mode = 0666

CentOS7中LNMP环境怎么搭建

最后,把三个参数修改完成后:wq退出然后重启php-fpm服务

sudo systemctl start php-fpm #启动php-fpm
sudo systemctl enable php-fpm #开机启动fpm

然后,我们来修改nginx的配置,先使用find命令查找配置文件位置,我的配置文件位置如下图

find / -name nginx.conf

CentOS7中LNMP环境怎么搭建

然后,使用vi 命令进入查看,在最后一行发现这个配置文件又引入了其他配置文件。

vi /etc/nginx/nginx.conf

CentOS7中LNMP环境怎么搭建

再次进入这个目录发现配置文件如下图

CentOS7中LNMP环境怎么搭建

使用vi命令修改它

vi default.conf

在localhost下加上同级,如下图所示

location ~ \.php$ {
root /var/www/html; #指定php的根目录
fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
fastcgi_index index.php;
fastcgi_param script_filename $document_root$fastcgi_script_name;
include fastcgi_params;
}

CentOS7中LNMP环境怎么搭建

修改保存之后,使用nginx -t命令确认格式无错误,后重启nginx。如下图所示

nginx -tnginx -s reload

之后,在刚刚设置的php目录下,新建一个php文件用于测试。

在/var/www/html建立index.php

<?php

phpinfo();

CentOS7中LNMP环境怎么搭建

然后,我们访问localhsot/index.php如果看到以下画面,则说明我们的nginx php 已经关联上了。

CentOS7中LNMP环境怎么搭建

读到这里,这篇“CentOS7中LNMP环境怎么搭建”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI