基础环境:
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相连接;