VMware CentOS7 配置 LAMP 环境


0. 准备

下载安装 VMware。
下载安装 CentOS。

VMware 需要设置成桥接模式 设置 -> 网络适配器 -> 桥接模式网络连接 -> 自动检测
CentOS 需要打开有线连接 设置 -> 网络 -> 有线

安装时添加的 centos 用户没有 sudo 权限,需要手动添加:

su root
vim /etc/sudoers

root ALL=(ALL) ALL 之后添加 你的用户名 ALL=(ALL) ALL:wq! 保存退出。

开启 vim 行号:

vim /etc/vimrc

最后添加 set nu

1. 安装 Apache

apache 的安装配置:

yum install httpd # 安装
systemctl enable httpd # 开机自启
apachectl restart # 重启 apache,也可用 systemctl reload httpd

配置防火墙:

firewall-cmd --add-service=http --permanent # 防火墙添加 http 服务
systemctl restart firewalld # 重启防火墙

现在使用mac的浏览器打开虚拟机IP http://192.168.1.192/ 能看到 apache 的 Testing 123.. 页面。

2. 安装 php

安装:

yum install php

修改 mac 或者 路由器 的 host 192.168.1.192 sample.com

在 apache 默认目录 /var/www/html 下创建一个文件夹 sample,sample 下新建一个文件 index.php 内容为:

<?php phpinfo() ?>

配置虚拟主机:

vim /etc/httpd/conf.d/vhost.conf

添加内容:

<VirtualHost *:80>

    ServerAdmin xxx@phphp.net
    ServerName sample.com
    ServerAlias www.sample.com

    DocumentRoot /var/www/html/sample
    #ErrorLog "/var/www/html/sample/sample-error_log"
    #CustomLog "/var/www/html/sample/sample-access_log" common

    <Directory "/var/www/html/sample">

        # 我常用的 rewrite 规则
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|images|robots\.txt)
        RewriteRule ^(.*)$ /index.php/$1 [L]

    </Directory>

</VirtualHost>

重启 apache:

apachectl restart

现在打开 sample.com 就能看到 phpinfo,至此 LAMP 中只剩下 mysql 还没安装。

3. 安装 MariaDB (MySQL)

CentOS7 默认使用 MariaDB,开源的 MariaDB 是 MySQL 的一个分支,MariaDB 目的完全兼容 MySQL。

安装:

yum install mariadb-server mariadb # 安装
systemctl enable mariadb # 开机自启
systemctl start mariadb # 启动
mysql_secure_installation # 配置 mysql
#mysql -u root -p # 登录 mysql
yum install php-mysql # 安装 php-mysql 使得 php 能使用 mysql
apachectl restart # 重启 apache

现在再开打 sample.com 能看到 phpinfo 中包含了 mysql 支持。


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注