Linux安装LNMP(Linux+Nginx+MySQL+PHP)(centos7)

LAMP环境和软件版本

名称版本号查询命令
linux系统CentOS Linux release 7.7.1908 (Core)cat /etc/redhat-release
nginxnginx-1.16.1-1.el7.x86_64rpm -qa | grep nginx 或 nginx -v
mariadbmariadb-server-5.5.64-1.el7.x86_64rpm -qa | grep mariadb
phpphp-5.4.16-46.el7.x86_64rpm -qa | grep php

一、安装Nginx

1、查看是否安装过nginx。

rpm -qa | grep nginx

2、有就卸载nginx。

yum remove -y "nginx*"

3、重新安装nginx。

yum install -y nginx

4、查看启动状态。

systemctl status nginx

5、启动nginx。

systemctl start nginx

6、添加开机启动。

systemctl enable nginx

7、设置防火墙开放tcp80端口。

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

8、使用浏览器访问http://192.168.1.150/,显示如下界面,说明安装的nginx服务正常运行。
在这里插入图片描述

9、默认主页index.html位于默认目录/usr/share/nginx/html中。若要发布自己的网站内容,替换index.html主页内容即可。例:主页内写入内容 “This is a Nginx test.” 。使用浏览器访问http://192.168.1.150,则显示如下界面内容。若要更换主页目录位置,需在nginx配置文件/etc/nginx/nginx.conf中设置目录路径。

echo  "This is a Nginx  test."   >   /usr/share/nginx/html/index.html

在这里插入图片描述

二、安装mysql数据库

1、查看是否安装过mariadb。

rpm -qa | grep mariadb

2、有就卸载mariadb。

yum remove -y "mariadb*"

3、重新安装mariadb-server。

yum install -y mariadb-server

4、启动mariadb。

systemctl start mariadb

5、查看启动状态。

systemctl status mariadb

6、添加开机启动。

systemctl  enable  mariadb

7、设置mysql数据库root账号密码。

mysqladmin -uroot  password  'yourpassword'

mysql_secure_installation

在这里插入图片描述

8、root账号登陆mysql。

mysql -uroot -p

9、登陆mysql后可以使用如下命令重新设置当前账户数据库密码。

 MariaDB[(none)]> set password=password('123456');

10、创建一个新用户newuser,密码为123456,授权远程计算机使用账号newuser登陆数据库,并立刻刷新权限。

 MariaDB[(none)]> grant all on *.* to  'newuser'@'%' identified by '123456';
 MariaDB[(none)]> flush 	privileges;

上述语句表示使用"newuser"账户,"123456“”密码从任何主机连接到mysql服务器,并赋予所有的权限。

11、退出mysql数据库。

MariaDB[(none)]> quit; 

 MariaDB[(none)]> exit;

12、设置防火墙开放tcp3306端口。

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

在这里插入图片描述

13、远程计算机连接服务器数据库时使用如下命令,输入密码即可登录mysql数据库。

mysql -unewuser -p -h 192.168.1.150  -P  3306

三、安装PHP

1、查看是否安装过php。

rpm -qa | grep php

2、有就卸载php。

yum remove -y "php*"

3、重新安装php。

yum install -y php

4、创建文件/usr/share/nginx/html/index.php,写入内容 “<?php phpinfo(); ?>”。

touch    /usr/share/nginx/html/index.php
echo  "<?php  phpinfo();  ?>" > /usr/share/nginx/html/index.php

5、重启nginx服务,浏览器访问http://192.168.1.150,显示如下图,则说明php安装正常,并且可以解析php文件。

在这里插入图片描述

四、nginx环境无法解析php文件,一访问就下载php文件解决方法

1、查看php-fpm是否安装。

rpm -qa |grep  "php-fpm"

2、安装并启动php-fpm。

yum install -y  php-fpm
systemctl  start  php-fpm

3、查找nginx配置文件,编辑nginx配置文件并保存,增加内容如下图红框。

find  /  -name   nginx.conf
vi   /etc/nginx/nginx.conf
		location / {
            index index.php  index.html index.htm;
        }

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

在这里插入图片描述

4、重启nginx和php-fpm。浏览器访问http://192.168.1.150,即可正确解析php文件。

systemctl   restart  nginx
systemctl   restart  php-fpm

在这里插入图片描述

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值