CentOS7中LNMP环境怎么搭建(centos7,lnmp,编程语言)

时间:2024-05-02 19:03:14 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

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

首先配置防火墙

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

1.关闭firewall:

systemctlstopfirewalld.service#停止firewallsystemctldisablefirewalld.service#禁止firewall开机启动

2.关闭selinux:

vi/etc/selinux/config

#selinux=enforcing #注释掉

selinux=disabled #增加

:wq! #保存退出

setenforce 0 #使配置立即生效

CentOS7中LNMP环境怎么搭建

3.安装priorities与wget

yuminstallyum-priorities-yyum-yinstallwget

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环境怎么搭建

开机启动

systemctlenablemysqldsystemctldaemon-reload

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

grep'temporarypassword'/var/log/mysqld.log

CentOS7中LNMP环境怎么搭建

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

mysql-uroot-p

CentOS7中LNMP环境怎么搭建

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

setpasswordfor'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>showvariableslike‘%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>grantallonroot.*toroot@'%'identifiedby'vmroot!@#456vmroot';mysql>flushprivileges;mysql>exit;

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

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

配置默认编码为utf8

vi/etc/my.cnf

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

[mysqld]character_set_server=utf8init_connect='setnamesutf8'

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

CentOS7中LNMP环境怎么搭建

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

关闭firewall:

systemctlstopfirewalld.service#停止firewallsystemctldisablefirewalld.service#禁止firewall开机启动

安装iptables防火墙:

yuminstalliptables-services#安装sudovi/etc/sysconfig/iptables#编辑防火墙配置文件

配置文件更改如下:

#firewallconfigurationwrittenbysystem-config-firewall#manualcustomizationofthisfileisnotrecommended.*filter:inputaccept[0:0]:forwardaccept[0:0]:outputaccept[0:0]-ainput-mstate--stateestablished,related-jaccept-ainput-picmp-jaccept-ainput-ilo-jaccept-ainput-mstate--statenew-mtcp-ptcp--dport22-jaccept//下面是编辑添加的部分-ainput-mstate--statenew-mtcp-ptcp--dport80-jaccept-ainput-mstate--statenew-mtcp-ptcp--dport3306-jaccept//以上是编辑添加的部分-ainput-jreject--reject-withicmp-host-prohibited-aforward-jreject--reject-withicmp-host-prohibitedcommit

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

systemctlrestartiptables.service#最后重启防火墙使配置生效systemctlenableiptables.service#设置防火墙开机启动

2、关闭selinux

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

sudovi/etc/selinux/config

修改内容如下

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

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

setenforce0#使配置立即生效

2.安装php

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

yuminstallepel-releaserpm-ivhhttp://rpms.famillecollet.com/enterprise/remi-release-7.rpmyuminstall--enablerepo=remi--enablerepo=remi-php56phpphp-opcachephp-fpmphp-develphp-mbstringphp-mcryptphp-mysqlndphp-phpunit-phpunitphp-pecl-xdebugphp-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

systemctlstartnginx.service#启动nginxsystemctlstopnginx.service#停止systemctlrestartnginx.service#重启systemctlenablenginx.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配置文件

sudovi/etc/php-fpm.d/www.conf

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

listen.owner=nginxlisten.group=nginxlisten.mode=0666

CentOS7中LNMP环境怎么搭建

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

sudosystemctlstartphp-fpm#启动php-fpmsudosystemctlenablephp-fpm#开机启动fpm

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

find/-namenginx.conf

CentOS7中LNMP环境怎么搭建

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

vi/etc/nginx/nginx.conf

CentOS7中LNMP环境怎么搭建

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

CentOS7中LNMP环境怎么搭建

使用vi命令修改它

videfault.conf

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

location~\.php${root/var/www/html;#指定php的根目录fastcgi_pass127.0.0.1:9000;#php-fpm的默认端口是9000fastcgi_indexindex.php;fastcgi_paramscript_filename$document_root$fastcgi_script_name;includefastcgi_params;}

CentOS7中LNMP环境怎么搭建

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

nginx-tnginx-sreload

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

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

<?php

phpinfo();

CentOS7中LNMP环境怎么搭建

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

CentOS7中LNMP环境怎么搭建

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:CentOS7中LNMP环境怎么搭建的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:CentOS7 LNMP+phpmyadmin环境搭建的方法下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18