CentOS7用yum搭建LAMP环境
一、实验环境
[root@real ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@real ~]# uname -a
Linux real 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
二、安装apache
[root@real ~]# yum install httpd httpd-devel -y
启动apache服务
[root@real ~]# systemctl start httpd
查看服务状态
[root@real ~]# systemctl status httpd
设置apache服务开机自启
[root@real ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
查看80端口监听中
#ss跟netstat功能相同
[root@real ~]# ss -lntup |grep httpd
tcp LISTEN 0 128 :::80 :::* =4929,fd=4),("httpd",pid=4928,fd=4),("httpd",pid=4927,fd=4),("httpd",pid
ss命令具体了解https://blog.csdn.net/hustsselbj/article/details/47438781
关闭防火墙
[root@real ~]# systemctl stop firewalld
或者
防火墙设置开启80端口
[root@real ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@real ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@real ~]# firewall-cmd --reload
success
浏览器访问
三、安装mariadb
安装mariadb及依赖
[root@real ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
[root@real ~]# rpm -qa | grep mariadb
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-devel-5.5.60-1.el7_5.x86_64
启动mariadb
[root@real ~]# systemctl start mariadb
设置开机自启
[root@real ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
查看3306端口
[root@real ~]# ss -lntup | grep mysqld
tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=1776,fd=13))
#数据库安全设置
[root@real ~]# 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] n #设置root用户可以远程登录数据库
... skipping.
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!
进入数据库测试
[root@real ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.60-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.00 sec)
四、安装PHP
[root@real ~]# yum install php -y
查看php生成文件路径
[root@real ~]# rpm -ql php
/etc/httpd/conf.d/php.conf
/etc/httpd/conf.modules.d/10-php.conf
/usr/lib64/httpd/modules/libphp5.so
/usr/share/httpd/icons/php.gif
/var/lib/php/session
php与mysql连接
[root@real ~]# yum install php-mysql -y
[root@real ~]# rpm -ql php-mysql
/etc/php.d/mysql.ini
/etc/php.d/mysqli.ini
/etc/php.d/pdo_mysql.ini
/usr/lib64/php/modules/mysql.so
/usr/lib64/php/modules/mysqli.so
/usr/lib64/php/modules/pdo_mysql.so
安装常用的PHP模块
[root@real ~]# 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
在/var/www/html/下创建info.php文件,测试php
[root@real ~]# cd /var/www/html/
[root@real html]# vim info.php
<?php
phpinfo();
?>
重启apache
[root@real html]# systemctl restart httpd
php与mysql数据库连接测试
[root@real ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.60-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.00 sec)
MariaDB [(none)]> create database test; #创建数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on test.* to 'xiaozhang'@'localhost' identif
ied by '123456'
-> ;#授权给用户xiaozhang
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; #刷新
创建php与mysql连接文件
[root@real html]# vim test.php
<?php
$link_id=mysql_connect('localhost','xiaozhang','123456') or mysql_error();
if($link_id){
echo "mysql successful!\n";
}else{
echo mysql_error();
}
?>
浏览器访问
至此,完成!