Centos7.9 使用yum搭建LAMP平台

基础环境:

1)虚拟机需要联网

2)关闭防火墙和内核安全机制

[root@centos7 ~]# systemctl stop firewalld

[root@centos7 ~]# setenforce 0

一、安装http服务

1.1 安装apache 软件包

[root@centos7 ~]# yum install httpd httpd-devel -y

/var/www/html                    ###网站发布目录

/etc/httpd                        ###httpd服务的根目录

/var/log/httpd/access_log           ###存放访问日志

/var/log/httpd/error_log            ###存放错误日志

/etc/httpd/conf/httpd.conf          ###主配置文件

  

[root@centos7 ~]# egrep -v  "#|^$" /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"                              ###服务目录 

Listen 80                                          ###在所有IP地址上监听端口

User apache                                       ###运行服务的用户

Group apache                                      ###运行服务的用户组

ServerAdmin root@localhost                          ###管理员邮箱

ServerName www.example.com:80                     ###设置网站域名

DocumentRoot "/var/www/html"                      ###网站发布目录

ErrorLog "logs/error_log"                             ###错误日志文件

LogLevel warn                                      ###日志级别

CustomLog "logs/access_log" combined                 ###正常访问日志

AddDefaultCharset UTF-8                             ###网页默认字符集

1.2 启动apache服务

[root@centos7 ~]# systemctl  start   httpd

  

1.3 添加httpd服务为开机启动

[root@centos7 ~]# systemctl  enable  httpd

1.4 查看服务状态

[root@centos7 ~]# systemctl  status  httpd

1.5 若firewalld防火墙开启,则防火墙开放80端口和443端口

[root@centos7 ~]# firewall-cmd --permanent --zone=public  --add-service=http

success

[root@centos7 ~]# firewall-cmd --permanent --zone=public  --add-service=https

success

[root@centos7 ~]# firewall-cmd --reload

Success

1.6 查看80端口监听中

[root@centos7 ~]# netstat -anpt |grep :80

tcp6       0      0 :::80               :::*           LISTEN         72000/httpd

1.7 查看服务器IP地址

[root@centos7 ~]# ip addr

1.8 测试登录

浏览器地址栏输入http://192.168.10.129 ,则看到测试网页。

   

 

二、安装mysql服务

2.1 安装mysql软件包

[root@centos7 ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y

2.2 开启mysql服务,并设置为开机启动

[root@centos7 ~]# systemctl  start    mariadb

[root@centos7 ~]# systemctl  enable  mariadb

2.3 查看端口号

[root@centos7 ~]# netstat -anpt |grep mysqld

tcp        0      0 0.0.0.0:3306      0.0.0.0:*         LISTEN      73231/mysqld

2.4数据库初始化设置

[root@centos7 ~]# 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):         ##Enter回车

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                 ##输入 y  回车

New password: pwd123                       ##mysql登录密码

Re-enter new password: pwd123

Password updated successfully!

Reloading privilege tables..

... Success!

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

... Success!

Disallow root login remotely? [Y/n] n             ##允许root用户远程登录服务器

... skipping

Remove test database and access to it? [Y/n] y     ##删除test测试数据库

 - Dropping test database...

 ... Success!

 - Removing privileges on test database...

 ... Success!

Reload privilege tables now? [Y/n] y              ##立即刷新权限表

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

2.5登陆数据库测试

[root@centos7 ~]# mysql -u root -p

Enter password: pwd123                      ##输入密码 pwd123                         

MariaDB [(none)]> show databases;             ##查看数据库中有哪些库

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

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

Bye

三、安装PHP服务

3.1 安装php软件包

[root@centos7 ~]#  yum -y install php

[root@centos7 ~]#  rpm -ql php

/etc/httpd/conf.d/php.conf

/etc/httpd/conf.modules.d/10-php.conf

/usr/lib/httpd/modules/libphp5.so

/usr/share/httpd/icons/php.gif

/var/lib/php/session

3.2 将php与mysql关联起来

[root@centos7 ~]#  yum install php-mysql -y

[root@centos7 ~]#  rpm -ql php-mysql

/etc/php.d/mysql.ini

/etc/php.d/mysqli.ini

/etc/php.d/pdo_mysql.ini

/usr/lib/php/modules/mysql.so

/usr/lib/php/modules/mysqli.so

/usr/lib/php/modules/pdo_mysql.so

3.3 安装PHP常用模块

    [root@centos7 ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl  curl-devel php-bcmath

3.4 测试PHP网页

[root@centos7 ~]#  cd  /var/www/html/

[root@centos7 html]#  vim  test1.php

<?php

phpinfo();

?>

[root@centos7 html]#  vim  test2.php

<?php

$link=mysqli_connect('localhost','root','pwd123');           

if($link) echo "恭喜你,数据库连接成功啦!!";                     

mysqli_close($link);                                           

?>

3.5重启apache服务器

[root@centos7 html]# systemctl restart httpd

3.6测试PHP网页

在浏览器中输入 http://192.168.10.129/test1.php,则可以看到页面内容;

 

在浏览器中输入 http://192.168.10.129/test2.php,则验证php程序和mysql相连接;

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CentOS 7.9是一种常用的Linux系统,而LAMP是一种常见的Web服务器环境,用于搭建个人博客等网站。下面是使用本地yum安装LAMP的步骤: 1. 首先,确保你已经安装了CentOS 7.9操作系统,并且已经配置好网络连接。 2. 安装Apache HTTP服务器: - 打开终端,使用root权限登录。 - 运行以下命令安装Apache: ``` yum install httpd ``` - 安装完成后,启动Apache服务并设置开机自启动: ``` systemctl start httpd systemctl enable httpd ``` 3. 安装MySQL数据库: - 运行以下命令安装MySQL: ``` yum install mariadb-server ``` - 安装完成后,启动MySQL服务并设置开机自启动: ``` systemctl start mariadb systemctl enable mariadb ``` - 运行MySQL安全脚本以提高安全性并设置root密码: ``` mysql_secure_installation ``` 4. 安装PHP: - 运行以下命令安装PHP及相关扩展: ``` yum install php php-mysql ``` - 安装完成后,重新启动Apache服务: ``` systemctl restart httpd ``` 5. 配置虚拟主机: - 打开Apache的默认配置文件: ``` vi /etc/httpd/conf/httpd.conf ``` - 在文件末尾添加以下内容,用于配置虚拟主机: ``` <VirtualHost *:80> ServerName your_domain DocumentRoot /var/www/html/your_domain <Directory /var/www/html/your_domain> AllowOverride All </Directory> </VirtualHost> ``` - 将`your_domain`替换为你的域名或IP地址,并保存文件。 - 创建网站根目录: ``` mkdir /var/www/html/your_domain ``` - 设置正确的文件权限: ``` chown -R apache:apache /var/www/html/your_domain chmod -R 755 /var/www/html/your_domain ``` 6. 完成以上步骤后,你就可以通过浏览器访问你的个人博客了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值