LAMP环境的搭建和配置

安装LAMP,基础配置解析PHP

  • LAMP的搭建
  1. 安装MySQL(将所有的软件包放到/usr/local/src目录下

[root@localhost src]# tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz

[root@localhost src]# useradd -s /sbin/nologin mysql

                  //建立MySQL用户,因为启动MySQL需要该用户

[root@localhost src]# mkdir -p /data/mysql

                        //创建datadir,数据库文件会放到这里面

[root@localhost src]# chown -R mysql:mysql /data/mysql

                  // 更改权限,不更改后续操作就会出问题

[root@localhost src]# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql

                            // 将软件包移动到上一级目录下

[root@localhost src]# cd /usr/local/mysql

[root@localhost mysql]# yum install -y gcc-c++ gcc perl-Module-Install

                            // 下载编译所需要的环境

[root@localhost mysql]#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

                //--user表示定义数据库的以哪个用户的身份运

//--datadir表示定义数据库的安装目录

[root@localhost mysql]# cp support-file/my-default.cnf /etc/my.cnf

[root@localhost mysql]# vim /etc/my.cnf

                        // 修改配置文件

将配置文件修改成以下形式:

basedir = /usr/local/mysql   // basedir表示MySQL包所在路径

datadir = /data/mysql         //表示定义存放数据的位置

port = 3306 //port表示定义MySQL服务监听的端口号

server_id = 100           //表示该MySQL服务的id号

socket = /tmp/mysql.sock       //定义MySQL服务监听的套接字地址

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

                            //复制启动脚本

[root@localhost mysql]# vim /etc/init.d/mysqld

                            // 修改启动脚本

将启动脚本修改为一下行是:

basedir=/usr/local/mysql

datadir=/data/mysql

[root@localhost mysql]# chkconfig --add mysqld

                            // 把mysql服务加到系统服务列表中

[root@localhost mysql]# chkconfig mysqld on

                            // 开机自启

[root@localhost mysql]# service mysqld start

                            //启动服务

 安装成功

     LAMP环境的搭建和配置

 

2.安装Apache

 (1)安装与配置

[root@localhost src]# tar -zxvf apr-1.5.2.tar.gz

[root@localhost src]# tar -zxvf apr-util-1.6.1.tar

[root@localhost src]# tar -zxvf http-2.4.39.tar

[root@localhost src]# cd apr-1.5.2

[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr

[root@localhost apr-1.5.2]# make && make install

[root@localhost apr-1.5.2]# cd /usr/local/src/apr-util-1.5.4

[root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@localhost apr-util-1.5.4]# make && make install

[root@localhost apr-util-1.5.4]# cd /usr/local/src/httpd-2.4.49

[root@localhost src]# cp -r apr-1.5.2 httpd-2.4.49/srclib/apr

[root@localhost src]# cp -r apr-util-1.5.4 httpd-2.4.49/srclib/apr-util

[root@localhost src]# yum install -y pcre-devel

[root@localhost httpd-2.4.49]# ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most

//--enable-so表示启用DSO

//--enable -mods- shared表示以共享形式安装模块

[root@localhost httpd-2.4.49]# make -j4 && make install

[root@localhost httpd-2.4.49]# /usr/local/apache2.4/bin/apachectl -M

//查看配置文件有哪些文件

//share字样的动态共享模块,static以静态形式存在

 (2)安装成功

LAMP环境的搭建和配置

 

3.安装PHP

 (1)安装与配置

[root@localhost src]# tar -zxvf php-5.6.30.tar.gz

[root@localhost src]# yum install -y libxml2-devel openssl openssl-devel  bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel epel-release

[root@localhost src]# yum install -y libmcrypt-devel

[root@localhost src]# cd php-5.6.30

[root@localhost src]# ./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-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

[root@localhost php-5.6.30]# make -j4 && make install  //编译安装

4.httpd解析php

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf

                         //编辑httpd的主配置文件

搜索ServerName,把ServerName www.example.com:80前#去掉;

<Directory />

    AllowOverride none

    Require all denied

</Directory>

改成:

<Directory />

    AllowOverride none  

    Require all granted

</Directory>    //目的允许所有请求访问                  

搜索AddType application/x-gzip .gz .tgz,在下面添加一行 AddType application/x-httpd-php .php;

<IfModule dir_module>

    DirectoryIndex index.html

</IfModule>

改成:

<IfModule dir_module>

    DirectoryIndex index.html index.php

</IfModule>

测试安装:

测试配置文件是否正确

[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t

                         //检验配置文件是否正确:Syntax OK

[root@localhost ~]# /usr/local/apache2.4/bin/apachectl start

                        //查看是否启动

[root@localhost ~]# curl localhost

<html><body><h1>It works!</h1></body></html>

                        //使用curl命令简单测试,,显示了就是成功

检测是否正确解析PHP:

[root@localhost ~]# vim /usr/local/apache2.4/htdocs/1.php

<?

echo  “php解析正确”

?>

                          //编写一个测试脚本

[root@localhost ~]# curl localhost/1.php

php解析正确[root@localhost ~]#

                          //如显示此信息则PHP解析正确

安装成功:

LAMP环境的搭建和配置

 

LAMP环境的搭建和配置

 

 

  • Apache配置

  1.默认虚拟主机 

 (1)虚拟主机

虚拟主机(共享主机,又称虚拟服务器)是一种在单一主机或主机群上,实现多网域服

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

 (2)配置

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf

                 //将Include conf/extra/httpd-vhosts.conf的#号去掉

[root@localhost apache2.4]# mkdir docs

                 //创建存放一个网站的目录

[root@localhost apache2.4]# cd docs/

[root@localhost docs]# mkdir www.kz.com

                //创建网站的目录

[root@localhost docs]# mkdir 123.com

                //创建网站的目录

[root@localhost docs]# vim www.kz.com/index.html

[root@localhost docs]# vim 123.com/index.html

                //编辑网站首页的信息

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl -t

                //查看配置文件是否正确

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl graceful

                //刷新配置文件

[root@localhost ~]# curl -x127.0.0.1:80 www.ka.com

[root@localhost ~]# curl -x127.0.0.1:80 www.123.com

 (3)验证成功

LAMP环境的搭建和配置

 

 2. 用户认证

 (1)介绍

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

 (2)配置

[root@localhost docs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

                   // 配置文件编辑成以下内容      

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    ServerName kz.com

    ServerAlias www.ka.com

    <Directory /usr/local/apache2.4/docs/www.kz.com>

        AllowOverride AuthConfig

        AuthName "kz.com user auth"

        AuthType Basic

        AuthUserFile /usr/local/apache2.4/docs/htpasswd

        require valid-user

    </Directory>

    ErrorLog "logs/www.kz.com-error_log"

    CustomLog "logs/www.kz.com-access_log" common

</VirtualHost>

[root@localhost docs]# /usr/local/apache2.4/bin/htpasswd -cm /usr/local/apache2.4/docs/htpasswd kz

                   //生成密码文件,-c为creat,-m为指定密码加密的方式为MD5

                   // kz为用户名 ,输入密码

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl graceful

配置Windows上的hosts文件

加入以下内容:

192.168.2.100 www.kz.com www.123.com

(3)验证成功

LAMP环境的搭建和配置

 

3.域名跳转

(1)介绍

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

(2)配置

[root@localhost docs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

//将配置文件修改成以下内容:

 <VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    ServerName kz.com

    ServerAlias www.ka.com

    <IfModule mod_rewrite.c>

         RewriteEngine on          RewriteCond %{HTTP_HOST} !^www.kz.com$         RewriteRule ^/(.*)$ http://www.kz.com/$1 [R=301,L]

    </IfModule>

    ErrorLog "logs/www.kz.com-error_log"

    CustomLog "logs/www.kz.com-access_log" common

</VirtualHost>

[root@localhost docs]# vim /usr/local/apache2.4/conf/httpd.conf

                   //修改配置文件,搜索rewrite,把前面#去掉

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl -M |grep -i rewrite

rewrite_module (shared)

                   //有这行输出,说明正常加载rewrite模块

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl graceful

 (3)验证成功

可以看见状态码301,跳转的网址是www.kz.com

LAMP环境的搭建和配置

 

4. 访问日志

(1)介绍

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

(2)配置

[root@localhost docs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

                  //将配置文件改为以下内容:

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    ServerName kz.com

    ServerAlias www.ka.com

    <IfModule mod rewrite.c>

         RewriteEngine on

         RewriteCond %{HTTP_ HOST} !^www.123.com$

         RewriteRule ^/(.*)$ http://www.kz.com/$1 [R=301,L]

    </IfModule>

    ErrorLog "logs/www.kz.com-error_log"

    CustomLog "logs/www.kz.com-access_log" common

</VirtualHost>

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@localhost docs]# /usr/local/apache2.4/bin/apachectl graceful

[root@localhost ~]# curl -x127.0.0.1:80 www.kz.com -I

(3)验证成功

LAMP环境的搭建和配置

 

 5 .访问日记不记录静态文件

(1)配置

[root@localhost docs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

                  //将配置文件更改为以下内容:

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    ServerName kz.com

    ServerAlias www.ka.com

    <IfModule mod rewrite.c>

         RewriteEngine on

         RewriteCond %{HTTP_ HOST} !^www.123.com$

         RewriteRule ^/(.*)$ http://www.kz.com/$1 [R=301,L]

    </IfModule>

    SetEnvIf Request_URI ".*\.gif$" image-request

    SetEnvIf Request_URI ".*\.jpg$" image-request

    SetEnvIf Request_URI ".*\.png$" image-request

    SetEnvIf Request_URI ".*\.bmp$" image-request

    SetEnvIf Request_URI ".*\.swf$" image-request

    SetEnvIf Request_URI ".*\.js$" image- request

    SetEnvIf Request_URI ".*\.css$" image- request

    Customlog "logs/www.kz.com-access_log" combined

</VirtualHost>

[root@localhost image]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@localhost image]# /usr/local/apache2.4/bin/apachectl graceful

(2)验证成功

LAMP环境的搭建和配置

 

 6 .访问日记切割

(1)配置

[root@localhost docs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

                  //将配置文件更改为以下内容:

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    ServerName kz.com

    ServerAlias www.ka.com

    SetEnvIf Request_URI ".*\.gif$" image-request

    SetEnvIf Request_URI ".*\.jpg$" image-request

    SetEnvIf Request_URI ".*\.png$" image-request

    SetEnvIf Request_URI ".*\.bmp$" image-request

    SetEnvIf Request_URI ".*\.swf$" image-request

    SetEnvIf Request_URI ".*\.js$" image- request

    SetEnvIf Request_URI ".*\.css$" image- request

    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/www.kz.com-access_%Y%m%d.log 86400" combined env=!img

</VirtualHost>

[root@localhost image]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@localhost image]# /usr/local/apache2.4/bin/apachectl graceful

[root@localhost docs]# cd www.kz.com/

[root@localhost www.kz.com]# mkdir image

[root@localhost www.kz.com]# cd  image

[root@localhost image]# ls

Linux.png

[root@localhost image]# curl -x127.0.0.1:80 www.kz.com/image/Linux.png -I

[root@localhost image]# curl -x127.0.0.1:80 www.kz.com/Linux.txt -I

(2)验证成功

LAMP环境的搭建和配置

 LAMP环境的搭建和配置

 

7 .静态元素过期时间

(1)介绍

那到底能缓存多久呢?如果服务器上的某个图片更改了,那么应该访问新的图片才

对。这就涉及一个静态文件缓存时长的问题,也叫作“缓存过期时间”。在httpd的配置文件中,我们是可以控制这个时间的。

(2)配置

[root@localhost docs]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

                  //将配置文件更改为以下内容:

<VirtualHost *:80>

    ServerAdmin webmaster@dummy-host.example.com

    DocumentRoot "/usr/local/apache2.4/docs/www.kz.com"

    ServerName kz.com

    ServerAlias www.ka.com

         ExpiresByType image/gif  "access plus 1 days"

         ExpiresByType image/jpeg "access plus 24 hours"

         ExpiresByType image/png "access plus 24 hours"

         ExpiresByType text/css "now plus 2 hour"

         ExpiresByType application/x-javascript "now plus 2 hours"

         ExpiresByType application/javascript "now plus 2 hours"

         ExpiresByType application/x-shockwave-flash "now plus 2 hours"

         ExpiresDefault "now plus 0 min"

    </IfModule>

    ErrorLog "logs/www.kz.com-error_log"

    CustomLog "logs/www.kz.com-access_log" common

</VirtualHost>

[root@localhost image]# /usr/local/apache2.4/bin/apachectl -t

Syntax OK

[root@localhost image]# /usr/local/apache2.4/bin/apachectl graceful

[root@localhost image]# vim /usr/local/apache2.4/conf/httpd.conf

               //搜索关键词expires,找到这一行

#LoadModule expires_ module modules/mod_ expires .so

               //把前面#删除,重新加载服务

 (2)验证成功

LAMP环境的搭建和配置

 

上一篇:lamp


下一篇:LAMP环境搭建与配置