在centos8上配置LAMP环境

一、什么是LAMP

L指Linux操作系统,A指web服务器Apache httpd ,M指Maraiadb数据库/mySQL,P指php语言/python/perl。LAMP是这个web应用框架的总称。

二、安装

1、安装httpd,mariadb和php

[root@server18 ~] yum install -y httpd mariadb mariadb-server php

2、设置httpd和mariadb开机自启

[root@server18 ~] systemctl enable --now httpd
[root@server18 ~] systemctl enable --now mariadb
[root@server18 ~]

3、检查httpd是否正常启动

[root@server18 yum.repos.d] echo "success!" > /var/www/html/test.html
[root@server18 yum.repos.d] curl -I http://127.0.0.1/test.html
[root@server18 yum.repos.d] success!

4、关闭防火墙,selinux设置为宽容模式

[root@server18 ~] systemctl stop firewalld.service
[root@server18 ~] setenforce 0

5、配置mariadb数据库

[root@server18 yum.repos.d] mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y   #设置root密码
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y      #移除匿名用户
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y     #是否允许root用户远程登录
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y   #移除测试数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  #重载数据表权限
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

6、查看数据库服务是否启动

[root@server18 yum.repos.d] ss -anput | grep 3306
tcp   LISTEN 0      80                   *:3306               *:*    users:(("mysqld",pid=5484,fd=22))                                                                     

7、尝试登录数据库

[root@server18 yum.repos.d] mysql -u root -p12345 -h localhost
#回显如下
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;   #查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.007 sec)

MariaDB [(none)]> exit   #退出数据库
Bye

8、验证php是否正确安装,查看php版本

PHP-FPM 即 PHP FastCGI 进程管理器,是php中所使用的一种fastCGI。
随着 Web 的发展,Web 所能呈现的内容更加丰富,与用户的交互日益频繁,这个时候仅仅通过静态资源已经无法满足 Web 通信的需求,所以引入 CGI 以便客户端请求能够触发 Web 服务器运行另一个外部程序,客户端所输入的数据也会传给这个外部程序,该程序运行结束后会将生成的 HTML 和其他数据通过 Web 服务器再返回给客户端(即动态请求,比如基于 PHP、Python、Java 实现的应用)。利用 CGI 可以针对用户请求动态返回给客户端各种各样动态变化的信息。

[root@server18 ~] php -verson #查看php版本
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[root@server18 ~] systemctl status php-fpm.service   #查看php-fpm的状态

9、重启httpd和php-fpm

[root@server18 ~] systemctl restart httpd
[root@server18 ~] systemctl restart php-fpm.service

三、验证LAMP环境是否可用

/etc/httpd/conf.d/目录下,有一个名为php.conf的配置文件,此文件的存在,使httpd服务器在接受到php文件时,可以将PHP文件交给php解释器进行执行。

[root@server18 ~] ls /etc/httpd/conf.d/
autoindex.conf  php.conf  README  userdir.conf  welcome.conf

(一)、使用函数phpinfo();打印php的信息

1、在/var/www/html/下新建一个名为test.php的文件,phpinfo();这个函数会打印但钱所用的php的版本等配置信息。

[root@server18 ~] vim /var/www/html/test.php
[root@server18 ~] cat /var/www/html/test.php
<?php
phpinfo();
?>

2、浏览器访问,结果如下:
在这里插入图片描述

(二)、编写php文件连接数据库

1、安装php连接数据库的模块

[root@server18 ~] yum -y install php-mysqli
[root@server18 ~] php -m | grep mysql   #检查是否正确安装,回显如下
mysqli
mysqlnd
pdo_mysql

2、登录数据库,新建一个测试用户,用于测试是否可以使用php文件连接数据仓库
数据库新建用户命令为:CREATE USER '用户名'@'host IP' IDENTIFIED BY '密码'; ,host IP就是当前安装数据库的主机ip。

[root@server18 ~] mysql -u root -p12345
MariaDB [(none)]> CREATE USER 'webapp'@'192.168.189.100' IDENTIFIED BY '12345';       #新建一个用户,用户名为webapp,hostip为192.168.189.100,密码为12345
Query OK, 0 rows affected (0.000 sec)   #回显,表示用户已经正确创建

MariaDB [(none)]> exit   #退出数据库
Bye

3、编辑一个连接数据库的php文件

[root@server18 ~] vim  /var/www/html/connect_db.php
[root@server18 ~] cat /var/www/html/connect_db.php 
<?php
$mysql_c=mysqli_connect('192.168.189.100','webapp','12345');  #此处账号密码使用上一步新建的那个测试用户
if($mysql_c)
        echo "webapp connect successfully!";  #连接成功时打印
else
        echo "sorry";    #连接失败时打印
mysqli_close($mysql_c)
?>

4、重启httpd和php-fpm

[root@server18 ~] systemctl restart httpd
[root@server18 ~] systemctl restart php-fpm.service

5、浏览器验证是否可以连接
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值