LAMP 环境搭建与配置

安装 LAMP,基础配置解析 PHP

安装 MySQL

(1)下载
cd /usr/local/src	//软件包都放在这里方便管理
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz		//下载源码包:mysql-5.6.43-linux-glibc2.12-x86_64.tar
(2)解压
tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz		//解压
mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql		// 挪动位置
(3)安装和配置
cd /usr/local/mysql
useradd -s /sbin/nologin mysql  //建立 MySQL 用户,因为启动 MySQL 需要该用户
mkdir -p /data/mysql  //创建 datadir,数据库文件会放到这里面
chown -R mysql:mysql /data/mysql  // 更改权限,不更改后续操作就会出问题
ls -la /data/mysql/
#[ -d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old	//&&相当于一个判断,意前面命令执行才会执行后面
yum install -y perl_Module_Install
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql	//--user 表示定义数据库的以哪个用户的身份运 --datadir 表示定义数据库的安装目录
cp support-files/my-default.cnf /etc/my.cnf
vi /etc/my.cnf		//修改配置文件
	Basedir=/usr/local/mysql	//basedir 表示 MySQL 包所在路径
	Datadir=/data/mysql/datadir		//表示定义存放数据的位置
	Port=3306	//port 表示定义 MySQL 服务监听的端口号
	Server_id=128	//表示该 MySQL 服务的 ID 号
	socket = /tmp/mysql.sock	//定义 MySQL 服务监听的套接字地址
cp support-files/mysql.server /etc/init.d/mysql	//复制启动脚本文件
chmod 755 /etc/init.d/mysql	//修改启动脚本文件的属性
vim /etc/init.d/mysql		//修改启动脚本
	basedir=/usr/local/mysql
	datadir=/data/mysql
chkconfig --add mysql		//把 mysql 服务加到系统服务列表中
chkconfig mysql on		//开机就启动
service mysql start	//启动服务
(4)安装成功
[root@localhost mysql]#yum install -y net-tools
[root@localhost mysql]# netstat -ltunp|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      11782/mysqld        
[root@localhost mysql]# ps -ef | grep mysql
root      11605      1  0 04:19 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid
mysql     11782  11605  0 04:19 pts/0    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root      11844   1339  0 04:25 pts/0    00:00:00 grep --color=auto mysql
(5)出现问题及解决方法
[root@localhost mysql]# service mysql start
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
.. ERROR! The server quit without updating PID file (/data/mysql/localhost.localdomain.pid).
[root@localhost mysql]# mv /etc/my.cnf /etc/my.cnf.bak
[root@localhost mysql]# service mysql start
Starting MySQL. SUCCESS! 

安装 Apache

(1)下载
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.39.tar.gz
wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz 
wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.5.tar.gz 
(2)解压
tar -zxvf httpd-2.4.39.tar.gz 
tar -zxvf apr-1.6.5.tar.gz 
tar -zxvf apr-util-1.6.1.tar.gz 
(3)安装和配置
cd /usr/local/src/apr-1.6.5
./configure --prefix=/usr/local/apr
yum install -y libtool*
make -j4 && make install
cd ../apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
yum install -y expat-devel
echo $?		//返回值为0正确
make &&make install
cd ../httpd-2.4.39
cp -r /usr/local/src/apr-1.6.5 /usr/local/src/httpd-2.4.39/srclib/apr
cp -r /usr/local/src/apr-util-1.6.1 /usr/local/src/httpd-2.4.39/srclib/apr-util
yum install -y  pcre-devel
./configure --prefix=/usr/local/apache2.4 --enable-so --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make &&make install
(4)安装成功
[root@localhost apache2.4]# /usr/local/apache2.4/bin/httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (shared)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)
(5)出现问题及解决方法
[root@localhost apr-1.6.5]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.6.5
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/apr-1.6.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
yum groupinstall "Development Tools" "Server Platform Development" -y

安装 PHP

(1)下载
wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
(2)解压
#cd /usr/local/src
#tar -zxvf php-5.6.30.tar.gz
(3)安装和配置
yum install -y libxml2-devel
yum install -y openssl openssl -devel
yum install -y bzip2 bzip2-devel
yum install -y libpng libpng-devel
yum install -y freetype freetype-devel
yum install -y epel-release
yum install -y libmcrypt-devel
yum -y install libjpeg-devel
//为防止配置时出错提前安装库文件
cd php-5.6.30
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
make && make install
//编译安装
(4)安装成功
[root@localhost php-5.6.30]# /usr/local/apache2.4/bin/httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_prefork_module (shared)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)
 php5_module (shared)

httpd 解析 PHP

(2)编辑 httpd 的主配置文件
vim  /usr/local/apache2.4/conf/httpd.conf
	ServerName www.example.com:80
	Require all granted
	AddType application/x-httpd-php .php
	DirectoryIndex index.html index.php4
(2)测试安装
/usr/local/apache2.4/bin/apachectl -t	//检验配置文件是否正确:Syntax OK
/usr/local/apache2.4/bin/apachectl graceful		//启动 httpd 命令
[root@localhost ~]# ps -ef |grep http	//查看是否启动
root      76755      1  0 01:45 ?        00:00:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon    76764  76755  0 01:45 ?        00:00:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon    76765  76755  0 01:45 ?        00:00:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon    76766  76755  0 01:45 ?        00:00:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon    76767  76755  0 01:45 ?        00:00:00 /usr/local/apache2.4/bin/httpd -k graceful
daemon    76768  76755  0 01:45 ?        00:00:00 /usr/local/apache2.4/bin/httpd -k graceful
root      76772  76680  0 01:46 pts/1    00:00:00 grep --color=auto http
[root@localhost ~]# curl localhost		//使用 curl 命令简单测试,,显示了就是成功
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl localhost/1.php
php 解析正确[root@localhost ~]# 

Apache 配置

默认虚拟主机

(1)虚拟主机

        虚拟主机(共享主机,又称虚拟服务器)是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术。虚拟主机之间完全独立,并可由用户自行管理,虚拟并非指不存在,而是指空间是由实体的服务器延伸而来,其硬件系统可以是基于服务器群,或者单个服务器

(2)配置
vim /usr/local/apache2.4/conf/httpd.conf
	Include conf/extra/httpd-vhosts.conf
cp extra/httpd-vhosts.conf extra/httpd-vhosts.conf-bak
vim extra/httpd-vhosts.conf		//编辑虚拟主机配置文件
	<VirtualHost *:80>
		ServerAdmin webmaster@dummy-host.example.com		//ServerAdmin 指定管理员邮箱
    	DocumentRoot "/usr/local/apache2.4/docs/aaa.com"		//DocumentRoot 为该虚拟主机站点的根目录
    	ServerName aaa.com		//ServaerName 为网站的域名
    	ServerAlias www.aaa.com www.abc.com		//ServerAlias 为网站的第二域名
    	ErrorLog "logs/aaa.com-error_log"		//ErrorLog 为站点的错误日志
    	CustomLog "logs/aaa.com-access_log" common	//CustomLog 为站点的访问日志
	</VirtualHost>
cd /usr/local/apache2.4/
mkdir -p docs/aaa.com
echo “www.test0920.com” > usr/local/apache2.4/docs/aaa.com/index.html		//网站默认的主页就是 index.html 
/usr/local/apache2.4/bin/apachectl -t 
/usr/local/apache2.4/bin/apachectl graceful
(3)验证成功
[root@localhost docs]# curl -xlocalhot:80 www.aaa.com	//访问 www.aaa.com/index.html
www.test0920.com	
[root@localhost docs]# curl -xlocalhost:80 www.abc.com
www.test0920.com

用户认证

(1)介绍

        用户认证用来对某些目录中的网页进行访问控制,当用户访问这些页面的时候需要输入用户名和密码进行认证。

(2)配置
vim /usr/local/apache2.4/conf/extra/httpd-vhost.conf
	<VirtualHost *:80>
		<Directory /usr/local/apache2.4/docs/aaa.com>		// 指定认证的目录
		<FilesMatch admin.php>针对单个文件
			AllowOverride AuthConfig		// 这个相当于打开认证的开关
        	AuthName "abc.com user auth"		//自定义认证的名字,作用不大
        	AuthType Basic		//认证的类型,一般为 Basic
        	AuthUserFile /usr/local/apache2.4/docs/.htpasswd		//指定密码文件所在位置
 			require valid-user		// 指定需要认证的用户为全部可用用户
		</Directory>
		<FilesMatch admin.php>针对单个文件
			AllowOverride AuthConfig
       	    AuthName "abc.com user auth"
        	AuthType Basic
            AuthUserFile /usr/local/apache2.4/docs/.htpasswd
       		require valid-user
        </FilesMatch>
	</VirtualHost>
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
/usr/local/apache2.4/bin/htpasswd  -cm /usr/local/apache2.4/docs/.htpasswd ytl
//htpasswd 为创建用户的工具,-c为creat -m为指定密码加密的方式为 MD5,data/.htpasswd 为密码文件,
//aming 为创建的用户。第一次执行需加-c
//第二次不用加,否则密码文件会重置
curl -xlocalhost:80 aaa.com -I -u ytl:123123
(3)配置验证

进windows的C:\Windows\System32\drivers\etc配置hosts
192.168.52.138 aaa.com www.aaa.com //虚拟机IP 网址

(4)验证成功

在这里插入图片描述

域名跳转

(1)介绍

        当我们变更网站域名或者申多个域名指向一个网站的时候,这个时候我们就
会用到域名跳转。

(2)配置
<VirtualHost * :80>
DocumentRoot "/data/wwwroot/testdomain.com"
ServerName www.testdomain.com
ServerAlias testdomain.com
<IfModule mod_rewrite.c> //- 需要 mod_ _rewrite 模块 支持
RewriteEngine on	// 打开 rewrite 功能
RewriteCond %{HTTP_ HOST} !^www.testdomain.com$		//定义 rewrite 的条件,当主机名不是 www.testdomain.com 时满足条件
RewriteRule ^/(. *)$ http://www.testdomain.com/$1 [R=301,L]		//定义 rewrite 规则,RewriteRule 后面分为三个部分,第一部分为当前网址,第二部分为要跳转的网址,第三部分是选项,需要括号括起来。
</IfModule>
</VirtualHost>

要实现跳转,需要 rewrite 模块的支持,所以查看是否安装该模块。如果没有则还需要一些配置:

/usr/lkocal/apache2.4/bin/apachectl -M |grep -i rewrite		//如果没有输出则需要继续编辑
vim /usr/local/apache2.4/conf/httpd.conf		//搜索 rewrite,把前面#去掉
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
(4)验证成功4
[root@localhost extra]# curl -x127.0.0.1:80 aa.com -I 
HTTP/1.1 301 Moved Permanently
Date: Sat, 15 Aug 2020 12:28:33 GMT
Server: Apache/2.4.39 (Unix) PHP/5.6.30
Location: http://aaa.com/
Content-Type: text/html; charset=iso-8859-1

访问日志

(1)介绍

       访问日志作用很大,不仅可以记录网站的访问情况,还可以在网站有异常发生时帮助我们定位问题。

(2)配置

访问日志记录用户的每一个请求

 vim /usr/local/apache2.4/conf/httpd.conf	//搜索LogFormat
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
(3)配置验证
/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful
curl -x127.0.0.1:80 -I aaa.com
tail /usr/local/apache2.4/logs/aaa-access_log
(4)验证成功
[root@localhost logs]# tail -n 2 aaa.com-access_log  
127.0.0.1 - - [15/Aug/2020:20:28:25 +0800] "HEAD HTTP://aaa.com/ HTTP/1.1" 200 -
127.0.0.1 - - [15/Aug/2020:20:28:33 +0800] "HEAD HTTP://aa.com/ HTTP/1.1" 301 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值