LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
本文主要介绍LNMP安装步骤,数据库使用MariaDB

一、安装Nginx以及Nginx相关配置

1.安装gcc
gcc是linux下的编译器在此不多做解释,感兴趣的小伙伴可以去查一下相关资料,它可以编译 C,C++,Ada,Object C和Java等语言。
安装命令:yum -y install gcc
2.pcre、pcre-devel安装
pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库。
安装命令:yum install -y pcre pcre-devel
3.zlib安装
zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip
4.安装openssl
安装命令:yum install -y openssl openssl-devel
5.安装nginx
(1)下载nginx安装包
wget http://nginx.org/download/nginx-1.9.9.tar.gz
(2)把压缩包解压到/usr/local/java(目录可自行制定)
tar -zxvf nginx-1.9.9.tar.gz -C /usr/local/java
(3)切换到cd /usr/local/java/nginx-1.9.9/下面
./configure --user=nobody --group=nobody – prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
make&make install
(4)安装完成,切换到/usr/local/nginx/conf修改配置文件(自行根 据需求进行修改)

验证是否安装成功:在浏览器输入IP地址:端口 出现以下界面即安装成功

centos7 lnmp搭建与配置 centos安装lnmp环境_运维


将nginx添加到开机自启动

1.切换到/lib/systemd/system 目录下 vim nginx.service编辑以下内容

[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

2.保存退出,执行以下指令:systemctl enable nginx.service

二、安装Mariadb

因centos7 yum源自带的Mariadb为5.6版本(版本低,有安全漏洞),故修改yum源安装10.3版本

vim /etc/yum.repos.d/MariaDB.repo //修改yum源

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

保存退出

yum clean all //清除缓存
yum makecache //重新生成yum源

yum install MariaDB MariaDB-server MariaDB-devel -y //安装MariaDB

启动mariadb服务:
启动服务:systemctl start mariadb.service
添加到开机启动:systemctl enable mariadb.service
查看服务状态:systemctl status mariadb.service

进行安全设置
mysql_secure_installation
配置说明如下:
Enter current password for root (enter for none):<一初次运行直接回车
Set root password?[Y/n] <一是否设置root密码,输入y并回车或者直接回车
New Password:<一设置root密码
Re-enter new password:<一再次输入设置的root密码
Remove anonymous users?[Y/n] <一是否删除匿名用户
Disallow root login remotely?[Y/n] <一是否禁止root远程登录
Remove test database and access to it?[Y/n]<一是否删除test数据库
Reload privilege tables now?[Y/n]<一是否重新加载权限表

全部设置完之后即可登录数据库:mysql -uroot -p

三、安装PHP

1.安装epel yum install epel-release

2.安装remi yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

3.安装yum-utils yum install yum-utils

4.Enable remi repo yum-config-manager --enable remi-php72

5.安装php7.2 yum install php72

yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache

6.查看版本:php -v

7.配置nginx

编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页

然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:

centos7 lnmp搭建与配置 centos安装lnmp环境_centos7 lnmp搭建与配置_02


重启nginx服务,

在/usr/local/nginx/html目录下创建index.php文件并编辑: <?php phpinfo(); ?>

然后打开浏览器输入ip/index.php 出现php版本信息说明LNMP环境已经搭建

centos7 lnmp搭建与配置 centos安装lnmp环境_mysql_03