安装mysql

[root@localhost ~]# yum install -y wget

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

[root@localhost ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.0/mysql-5.0.96-linux-i686-glibc23.tar.gz

[root@localhost ~]# tar zxvf mysql-5.0.96-linux-i686-glibc23.tar.gz

[root@localhost ~]# mv mysql-5.0.96-linux-i686-glibc23/ /usr/local/mysql

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

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

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

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

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

[root@localhost ~]# /bin/cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf

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

[root@localhost ~]# chmod 755 /etc/init.d/mysqld

[root@localhost ~]# vi /etc/init.d/mysqld

basedir=/usr/local/mysql

datadir=/data/mysql

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

[root@localhost ~]# chkconfig mysqld on

[root@localhost ~]# /etc/init.d/mysqld restart

[root@localhost ~]# ps aux | grep mysqld


配置mysql

查看mysql信息

[root@localhost ~]# cat /usr/local/mysql/bin/mysqlbug

mysql主配置文件

[root@localhost ~]# ll /etc/my.cnf

设置mysql用户root密码为123456

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -uroot password '123456'

以root用户123456密码登陆

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p123456

重置mysql root密码

1、在主配置文件内加入配置

skip-garnt

2、重启mysql

[root@localhost ~]# /etc/init.d/mysqld restart

3、直接登陆mysql,此时登陆不要求输入用户密码

[root@localhost ~]# /usr/local/mysql/bin/mysql

以下为mysql数据库操作

4、更改root用户密码为123456

update user set password=password('123456') where user='root';

成功后,把/etc/my.cnf配置文件内skip-garnt这个配置项注释或者删除

[root@localhost ~]# /etc/init.d/mysqld restart

5、mysql数据库操作

select user();  查看当前登陆用户

select database();  查看当前所操作的数据库

select version();  查看mysql版本

show create table host\G;  查看host表的创建语句

客户端远程登陆服务端mysql

[root@localhost ~]# /usr/local/mysql -uroot -h192.168.10.28 -P3306 -p123456

-uroot  用户名root

-h  远程服务器IP

-P3306  mysql端口

-p123456  密码

grant all on *.* to 'root'@'192.168.10.11' identified by '123456'

允许客户端远程登陆操作mysql

all    表示所有权限,权限有select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个

第一个*号    表示所有数据库

第二个*号    表示所有数据表

root         授权的mysql用户

192.168.10.11    授权的客户端IP

123456        密码

flush privileges;    刷新表

show processlist;    查看队列

show variables;      查看变量

set global max_connections=200;    临时设置变量值

show variables like 'max_connections';    查看指定变量值

show variable;    查看mysql参数

show status;    查看状态

show status like 'uptime';    查看指定参数状态

show status like 'key_read%';

show databases;    查看已经存在的数据库

create database test1;    新建test1数据库

drop database test1;    删除test1数据库

use test1;    进入test1数据库

show tables;    查看已经存在的表

create table testa(name varchar(20) not null);    新建testa表,表内有name列

describe testa;    查看表结构

alter table name add age varchar(10);    插入age列到name表中

alter table name drop age;    从表name中删除age列

alter table name add index age1(age);    从表name中添加索引

alter table name drop index age1;    从表name中删除索引

alter table name add primary key(age);    把age列添加为主键

alter table name drop primary key;    删除主键

alter table name rename name1;    重命名表

insert into name(name,age) values('Peter','11');    向表中插入数据

select * from name;    查看表内容

select age,name from name;    查看指定列内容

select name from name where age=22;    查看满足条件的内容

update name set name='Ben' where age=11;    修改表内容

delete from name;    清空表内容

delete from name where name='Sam';    删除满足条件的内容

drop table testa;    删除testa表

备份test数据库

[root@localhost ~]# /usr/local/mysql/bin/mysqldump -uroot -p111111 test > ~/test.sql

还原test数据库

[root@localhost ~]# /usr/local/mysql/bin/mysql -uroot -p111111 test < ~/test.sql


安装apache

[root@localhost ~]# yum install -y gcc zlib-devel

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

[root@localhost ~]# wget http://mirrors.sohu.com/apache/httpd-2.2.31.tar.gz

[root@localhost ~]# tar zxvf httpd-2.2.31.tar.gz

[root@localhost ~]# cd httpd-2.2.31

编译安装,--prefix指定安装目录,--enable-so表示启动DSO,--enable-deflate=shared表示共享方式编译deflate,后面的参数同理

[root@localhost ~]# ./configure -prefix=/usr/local/apache2 -with-included-apr -enable-so -enable-deflate=shared -enable-expires=shared -enable-rewrite=shared -with-pcre

[root@localhost ~]# make && make install


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

-t 测试语法

-l 列出静态模块

-M 列出静态及动态模块

graceful 平滑加载

start 启动

stop 停止

restart  重启


配置apache

查看程序信息

[root@localhost ~]# cat /usr/local/apache2/build/config.nice

apache主配置文件

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

监听端口

Listen 80

在配置文件内加这一行内容,使程序支持解析PHP文件

AddType application/x-httpd-php .php

指定apache程序目录

ServerRoot "/usr/local/apache2"

管理员邮箱

ServerAdmin you@example.com

网站根目录

DocumentRoot "/usr/local/apache2/htdocs"

指定执行程序的用户及用户组

User daemon

Group daemon

以下是程序加载的模块

LoadModule deflate_module modules/mod_deflate.so

LoadModule expires_module modules/mod_expires.so

LoadModule rewrite_module modules/mod_rewrite.so

LoadModule php5_module        modules/libphp5.so

指定索引页

<IfModule dir_module>

    DirectoryIndex index.html index.htm index.php

</IfModule>

在主配置文件内,把这行开头的#删除,启动虚拟主机

Include conf/extra/httpd-vhosts.conf


配置虚拟主机,精简配置

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>

    # 管理员邮箱

    ServerAdmin master@masker.net.cn

    # 网站根目录

    DocumentRoot "/usr/local/apache2/htdocs/masker.net.cn"

    # 网站域名

    ServerName masker.net.cn

    # 网址别名

    ServerAlias www.masker.net.cn

    # 错误日志

    ErrorLog "logs/masker.net.cn_error_log"

    # 访问日志

    CustomLog "logs/masker.net.cn_access_log" combined

</VirtualHost>


配置虚拟主机,当访问admin.html时需要用户认证,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

    <Directory /data/masker.net.cn/admin.html>

        AllowOverride AuthConfig

        AuthName "Auth"

        AuthType Basic

        AuthUserFile /data/.htpasswd

        require valid-user

    </Directory>

编辑完配置文件,执行以下命令,创建user1,如果再创建user2,不可以加参数-c

[root@localhost ~]# /usr/local/apache2/bin/htpasswd -c /data/.htpasswd user1


设置默认虚拟主机,限制只能通过指定域名访问。用户只能通过www.test.com和www.aaaa.com访问

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

NameVirtualHost *:80


<VirtualHost *:80>

DocumentRoot "/tmp/111"

ServerName 111.com

</VirtualHost>


<VirtualHost *:80>

    ServerAdmin master@test.com

    DocumentRoot "/usr/local/apache2/htdocs/"

    ServerName www.test.com

    ServerAlias www.aaaa.com

    ErrorLog "logs/www.test.com_error_log"

    CustomLog "logs/www.test.com_access_log" combined

</VirtualHost>


单域名301跳转,访问domain1.com时跳转到domain2.com,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<IfModule mod_rewrite.c>

    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^www.domain1.com$

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

</IfModule>


多域名301跳转,访问domain1.com或domain2.com、domain3.com时跳转到domain4.com,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<IfModule mod_rewrite.c>

    RewriteEngine on

    RewriteCond %{HTTP_HOST} ^www.domain1.com$ [OR]

    RewriteCond %{HTTP_HOST} ^www.domain2.com$ [OR]

    RewriteCond %{HTTP_HOST} ^www.domain3.com$

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

</IfModule>


加入配置后,使用以下命令检查rewrite_module模块有没有加载成功

[root@localhost ~]# /usr/local/apache2/bin/apachectl -M


apache日志配置,修改虚拟主机配置

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

    # 错误日志

    ErrorLog "logs/masker.net.cn_error_log"

    # 访问日志

    CustomLog "logs/masker.net.cn_access_log" combined

说明:

common 为日志格式,系统自带格式如下,也可自定义日志格式,在/usr/local/apache2/conf/httpd.conf文件中定义

[root@localhost ~]# grep LogFormat /usr/local/apache2/conf/httpd.conf

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %b" common


apache 日志切割,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/masker.net.cn_access_%Y%m%d_log 86400" combined    


apache 不记录指定类型日志,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

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

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

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

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

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

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

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

# 另外日志配置项后面要增加参数env=!p_w_picpath-request

CustomLog "logs/masker.net.cn_access_log" combined env=!p_w_picpath-request


apache 配置动态缓存,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<IfModule mod_expires.c>

    ExpiresActive on

    ExpiresByType p_w_picpath/gif "access plus 1 days"

    ExpiresByType p_w_picpath/jpeg "access plus 24 hours"

    ExpiresByType p_w_picpath/png "access plus 24 hours"

    ExpiresByType text/css "now plus 2 hours"

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

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

    ExpiresDefault "now plus 0 mon"

</IfModule>

测试(查看结果Cache-Control: max-age=86400 项):

[root@localhost extra]# curl -x127.0.0.1:80 'http://www.test1.com/1.jpg' -I

HTTP/1.1 200 OK

Date: Thu, 24 Dec 2015 10:54:40 GMT

Server: Apache/2.2.31 (Unix) PHP/5.3.28

Last-Modified: Mon, 14 Dec 2015 09:08:58 GMT

ETag: "80d1-844-526d805f09e80"

Accept-Ranges: bytes

Content-Length: 2116

Cache-Control: max-age=86400

Expires: Fri, 25 Dec 2015 10:54:40 GMT

Content-Type: p_w_picpath/jpeg


apache 配置防盗链,在<VirtualHost *:80>和</VirtualHost>中增加代码,其中http://domain1.com是名单,允许链接

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

SetEnvIfNoCase Referer "^http://.*\.domain1\.com" local_ref

<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png|js|css|swf)">

    Order Allow,Deny

    Allow from env=local_ref

</filesmatch>


apache 配置访问黑名单,禁止1.1.1.1这个IP访问,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<Directory "/data/masker.net.cn">

    AllowOverride None

    Order allow,deny

    Allow from all

    Deny from 1.1.1.1

</Directory>


apache 配置访问白名单,只允许127.0.0.1和192.168.1.2这2个IP访问文件名包含admin的文件,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<filesmatch "(.*)admin(.*)">

    Order deny,allow

    Deny from all

    Allow from 127.0.0.1

    Allow from 192.168.1.2

</filesmatch>


apache 配置禁止解析PHP(禁止访问PHP),在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<Directory /data/masker.net.cn>

    php_admin_flag engine off

    <filesmatch "(.*)php">

        Order deny,allow

        Deny from all

        Allow from 127.0.0.1

    </filesmatch>

</Directory>


apache 配置禁止指定user_agent,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<IfModule mod_rewrite.c>

    RewriteEngine on

    RewriteCond %{HTTP_USER_AGENT} ^.*curl.* [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} ^.*Mozilla/5\.0.* [NC]

    RewriteCond %{HTTP_USER_AGENT} ^.*Mozilla/4\.0.* [NC]

    RewriteRule .* - [F]

</IfModule>

说明:

RewriteEngine on

模块总开关

RewriteCond %{HTTP_USER_AGENT} ^.*curl.* [NC,OR]

匹配条件,其中NC表示不区分大小写,OR表示下面还有其他匹配条件

RewriteRule .* - [F]

表示拒绝

Mozilla/4\.0    是微软IE的user_agent

Mozilla/5\.0    是chrome的user_agent


apache 配置禁止访问指定内容,在<VirtualHost *:80>和</VirtualHost>中增加代码

[root@localhost ~]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_URI} ^.*/123/22.txt [NC]

RewriteRule .* - [F]

</IfModule>

上面是禁止访问指定文件

下面是禁止访问指定目录

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteCond %{REQUEST_URI} ^.*/123/.* [NC]

RewriteRule .* - [F]

</IfModule>


安装PHP

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

[root@localhost src]# wget http://mirrors.sohu.com/php/php-5.3.27.tar.gz

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

[root@localhost src]# vi /usr/local/apache2/bin/apxs

#!/replace/with/path/to/perl/interpreter -w

修改为

#!/usr/bin/perl -w

[root@localhost src]# cd php-5.3.27

[root@localhost php-5.3.27]# yum install -y epel-release

[root@localhost php-5.3.27]# yum install -y gcc zlib-devel perl libxml2-devel openssl openssl-devel bzip2 bzip2-devel libjpeg libjpeg-devel freetype freetype-devel libpng libpng-devel libmcrypt-devel

[root@localhost php-5.3.27]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/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-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif --disable-ipv6

[root@localhost php-5.3.27]# make && make install


配置PHP

PHP主程序文件

[root@localhost ~]# ll /usr/local/php/bin/php

使用php程序测试解析php文件

[root@localhost curl]# /usr/local/php/bin/php /usr/local/apache2/htdocs/1.php

查看PHP主配置文件所在路径

[root@localhost etc]# /usr/local/php/bin/php -i | grep Loaded

Loaded Configuration File => /usr/local/php/etc/php.ini

如果该文件不存在,则在安装包解压目录内拷贝PHP主配置文件

[root@localhost ~]# cp /usr/local/src/php-5.3.28/php.ini-production /usr/local/php/etc/php.ini


禁用部分危险函数

[root@localhost ~]# vi /usr/local/php/etc/php.ini

disable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellart,shell_exec,pr

oc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,chown,escapeshellcmd,escapeshellart,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,open

log,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close


配置错误日志

首先建立错误日志目录

[root@localhost ~]# mkdir -p /usr/local/php/logs ; chmod 777 /usr/local/php/logs/

保持以下配置项

[root@localhost ~]# vi /usr/local/php/etc/php.ini

display_errors = Off

log_errors = On

error_log = /usr/local/php/logs/

error_reporting = E_ALL & ~E_NOTICE


php配合apache配置网站根目录,在<VirtualHost *:80>和</VirtualHost>中增加代码,多个路径使:隔开

[root@localhost etc]# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

php_admin_value open_basedir "/usr/local/apache2/htdocs/:/tmp/"


PHP安装扩展模块

查看已经加载的模块

[root@localhost etc]# /usr/local/php/bin/php -m

假设我要添加curl模块,做法如下

[root@localhost etc]# cd /usr/local/src/php-5.3.28/ext/curl

[root@localhost ext]# yum install -y autoconf curl-devel

[root@localhost curl]# /usr/local/php/bin/phpize

[root@localhost curl]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@localhost curl]# make && make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

执行完以上命令之后,会提示extension_dir目录,也就是存放以上编译的模块存放的目录,这个参数可在/usr/local/php/etc/php.ini 文件内指定

查看extensions_dir

[root@localhost curl]# /usr/local/php/bin/php -i | grep extension_dir

extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626

在php主配置文件内增加如下内容,大概在986行

[root@localhost curl]# vi /usr/local/php/etc/php.ini

extension=curl.so

检查是否加载成功

[root@localhost curl]# /usr/local/php/bin/php -m | grep curl

curl

若未加载成功,可查阅日志

[root@localhost curl]# tail /usr/local/php/logs/php_errors.log

查看系统已经动态加载的模块

[root@localhost curl]# grep "^extension=" /usr/local/php/etc/php.ini


apache结合php

修改主配置文件

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

修改一,大概在313行,在AddType application/x-gzip .gz .tgz行下添加:

AddType application/x-httpd-php .php

修改二,大概在170行:

<IfModule dir_module>

    DirectoryIndex index.html

</IfModule>

修改为:

<IfModule dir_module>

    DirectoryIndex index.html index.php

</IfModule>

修改三,大概在102行:

#ServerName www.example.com:80

修改为:

ServerName localhost:80


测试apache语法

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

Syntax OK


启动apache

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


查看apache是否成功启动

[root@localhost ~]# netstat -nlp | grep httpd

tcp        0      0 :::80                       :::*                        LISTEN      1237/httpd


测试apache,结果如下,成功

[root@localhost ~]# curl localhost

<html><body><h1>It works!</h1></body></html>[root@localhost ~]#


测试php能否正确解析

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

<?php

echo "OK"

?>

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

php is OK


至此,LAMP搭建完毕。