LAMP架构搭建与优化

Mysql安装

 

cd /usr/local/sre/

wget   :hhttp://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-i686-glibc23.tar.gz

tar zxvf /usr/local/src/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
mv mysql-5.1.40-linux-i686-icc-glibc23 /usr/local/mysql
useradd -s /sbin/nologin mysql
cd /usr/local/mysql

mkdir -p /data/mysql ; chown -R mysql:mysql   /data/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
前两行 出现ok 表示安装成功

  可以输入 echo $?  出现结果0 表示安装成功

 ls 

 cd support-files/

ls

ls /etc/my.cnf

cp my-large.cnf  /etc/my.cnf

vim !$

ls /etc/init.d/
cp mysql.server /etc/init.d/mysqld
vim !$  更改  basedir=/usr/local/mysql   datadir=/data/mysql

chkconfig  --add mysqld
chkconfig  mysqld on
/etc/init.d/mysqld start

 1.2 Apache编译安装

 

cd /usr/local/src/

 wget  http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.31.tar.gz

cd http

 tar -zxvf  httpd-2.2.16.tar.gz
cd httpd-2.2.16
./configure \
--prefix=/usr/local/apache2 \
--with-included-apr \
--enable-so \
--enable-deflate=shared \
--enable-expires=shared \
--enable-rewrite=shared \
--with-pcre
若安装不上   报错  安装  yum -y -install gcc*

echo $?

yum install -y zlib-devel
yum install -y pcre pcre-devel apr apr-devel

make
make install

 ls /usr/local/apache2/

/usr/local/apache2/bin/apachetl -t检查apache有没有问题 syntaxt ok 表示没有问题

/usr/local/apache2/bin/apachetl graceful 重新加载配置文件

restart stop

/usr/local/apache2/bin/apachetl -l 工作模式

1.3 Apache的mpm工作模式

prefork  子进程 worker 线程 event  

ps aux |grep httpd

./configure/ -- help |grep -A3 'with-mpm

--with-mpm=worker

php编译安装-安装顺序在最后

 

cd /usr/local/src/

 wget http://cn2.php.net/get/php-5.4.45.tar.bz2/from/this/mirror  可加  -O  文件名.tar.bz2

  解压  tar jxvf 

 ./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-zlib-dir \
--with-bz2 \
--with-openssl \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-exif \
--disable-ipv6

 yum install -y libjpeg*

spacer.gif /usr/local/apache2/bin/apachectl start    restart

1.5 测试php解析

 

iptables  -F

 vim /usr/local/apache2/conf/httpd.conf

cd usr/local/apache2/htdocs

ls

/usr/local/php/bin/php 2.php

cp php.ini-production /usr/local/php/etc/php.ini
netstat -lnp |grep httpd 

  1.6 安装Discuz

mkdir /data/www

cd /data/www

wegt http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_GBK.zip

unzip D

ls

mv upload/* ./

rm -rf readme/ utillity/ upload/ Discuz_X3.2_SC_GBK.zip

 ps aux |grep httpd
vim /usr/local/apache2/conf/httpd.conf

<Directory "/usr/local/apache2/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all     更改

 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

 usr//local/apache2/bin/apachectl -t

 cat /usr/local/apache2/conf/httpd.conf |grep -i include (出错用)

 curl -x127.0.0.1:80 www.test.com/index.php   -I (出错用)

 chown -R daemon config data uc_client/data uc_server/data

which $PATH

which mysql

rpm -qf /usr/bin/mysql

 vim /etc/profile.d/path.sh  

 #!/bin/bash
export PATH=$PATH:/usr/local/mysql/bin
  source !$

 mysql

 which mysql    /usr/local/mysql/bin/mysql

mysql

create database discuz;
 grant all on discuz.* to 'aming'@'localhost' identified by 'aminglinux';

  Apache用户认证

cd /data/www

mkdir abc

cd abc

cp /etc/passwd ./12.txt
 vim /usr/local/apache2/conf/extra/httpd-vhosts.con   添加

<VirtualHost *:80>
    DocumentRoot "/data/www"
    ServerName www.test.com
    ServerAlias www.aaa.com
#    ErrorLog "logs/dummy-host.example.com-error_log"
#   CustomLog "logs/dummy-host.example.com-access_log" common
    <Directory /data/www/abc>
         Allowoverride AuthConfig
         AuthName "自定义的"
         AuthType Basic
        AuthUserFile /data/.htpasswd

    require valid-user   

     </Directory>
</virtualHost>
    DocumentRoot "/data/www"
    ServerName www.test.com
    ServerAlias www.aaa.com
#    ErrorLog "logs/dummy-host.example.com-error_log"
#   CustomLog "logs/dummy-host.example.com-access_log" common
    vim /etc/profile.d/path.sh   添加

export PATH=$PATH:/usr/local/mysql/bin:usr/local/apache2/bin

 !sour

 /usr/local/apache2/bin/htpasswd -c /data/.htpasswd user1

 cat /data/.htpasswd

/usr/local/apache2/bin/htpasswd  /data/.htpasswd user2 加C就覆盖了user1
/usr/local/apache2/bin/apachectl graceful

  默认虚拟主机

 

 vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 添加

<VirtualHost *:80>
    DocumentRoot "/tmp/123"
    ServerName   1111.com
</virtualHost>

  mkdir /tmp/123

  chmod 600 /tmp/123

/usr/local/apache2/bin/apachectl -t

 /usr/local/apache2/bin/apachectl restart

域名301跳转

vim 编辑配置 添加 保存 重启

 

<IfModule mod_rewrite.c>

RewriteEngine on

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

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

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

</IfModule>

 curl -x192.168.1.194:80 www.aaa.com/alkjljlkjljlljkdo -I

Apache日志切割

 

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 添加

 ErrorLog "logs/test.com-error_log"  没做分割
   CustomLog "|/usr/local/apache2/bin/rotabelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log 86400" combined
ls /usr/local/apache2/logs/

cd /usr/local/apache2/logs

cat test.com-access_log

后来出现问题 ip更换过 解决方法

 更改 win7 hosts 里的ip

 ls /usr/local/apache2/bin/rotabelogs  查看日志
tail /usr/local/apache2/logs/error_log

 unable to start piped log program '/usr/local/apache2/bin/rotabelogs -l /usr/loc                al/apache2/logs/test.com-access_%Y%m%d_log 86400': No such file or directory
Unable to open logs

 rm -f /usr/local/apache2/conf/extra/.httpd-vhosts.conf.swp

vi /usr/local/apache2/conf/extra/httpd-vhosts.conf  禁掉分割日志的配置
 /usr/local/apache2/bin/httpd -k start

 Apache不记录指定文件类型日志 添加

 

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/apache/bin/rotatelogs -l /usr/local/apache/logs/oem.discuz.qq.com-access_%Y%m%d.log 86400" combined env=!image-request

 Apache配置静态缓存

 

curl -x127.0.0.1:80 'http://www.test.com/static/image/common/loge.php' -I

Apache配置防盗链

 

vi /usr/local/apache2/conf/extra/httpd-vhosts.conf  添加

SetEnvIfNoCase Referer "^http://.*\.amlinglinux\.com" local_ref
    SetEnvIfNoCase Referer ".*\.lishiming\.net" local_ref
    SetEnvIfNoCase Referer "^$" local_ref
   <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
    Order Allow,Deny
    Allow from env=local_ref
</filesmatch>

Apache访问控制

 

<Directory /data/www/>

Order deny,allow

 Deny from all

 Allow from 127.0.0.1

</Directory>

 

 Apache禁止解析php   配置 添加

 

<Directory /data/www/data>

php_admin_flag engine off

<filesmatch "(.*)php">

Order deny,allow

Deny from all

</filesmatch>

</Directory

 Apache 禁止指定user_agent

<IfModule mod_rewrite.c>

RewriteEngine on

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

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

RewriteRule  ^/{.*}$ http://www.test.com/$1 [R301,L]

RewrieCond % {HTTP_USER_ACENT} ^.*curl.* [NC,OR]

RewrieCond % {HTTP_USER_ACENT} ^.*chromel.* [NC]

RewriteRule .* - [F]

</IfModule>

 Apache通过rewrite限制某个目录

 

 ls /data/www/data/cache/

 /data/www/

mkdir tmp/ 

vim 12.txt

配置里添加

<IfModule mod_rewrite.c>

 RewriteEngine on

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

 RewriteRule .* - [F]

</IfModule>

  php.ini配置文件详解

 

 /usr/local/php/bin/php -i |head

要是没有找到源代码 找  php.ini-production

 mkdir /usr/local/php/logs/

 chmod 777 !$

 配置disable_function disable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server,popen,proc_open,proc_close

vim /usr/local/php/etc/php.ini 加入或者更改

 display_error=off

log_errors=on

 error_log=/usr/local/php/logs/error.log

 error_reporting = E_ALL | E_STRICT

 open_basedir

vi /usr/local/apache2/conf/extra/httpd-vhosts.conf

php_admin_value open_basedir "/data/www/:/tmp/"

 php扩展模块如何安装

 

  /usr/local/php/bin/php -m

 cd /usr/local/src/php-5.4.45/
 cd  ext/

/usr/local/php/bin/php -m |grep -i curl

cd curl/
 /usr/local/php/bin/phpize

 ./configure --with-php-config=/usr/local/php/bin/php-config    rpm -qa |grep curl-devel 

 make

make install

ls 后跟上面的路径   /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

  /usr/local/php/bin/php -i |grep extension_dir

cp php.ini-production /usr/local/php/etc/php.ini

 tail /usr/local/php/logs/php_errors.log


 mysql配置讲解

 

vim /etc/my.cnf 

 mysql -uroot

  interactive_timeout = 8  

 wait_timeout = 8

long_query_time = 1                          慢查询日志

log_slow_queries = /data/mysql/slow.log

 mysql的root密码重置

 

  mysqladmin -uroot password 'aminglinux.com'

mysql -uroot -paminglinux.com
/etc/init.d/mysqld restart

/usr/local/mysql/bin/mysql -uroot

   vim /etc/my.cnf                                   在[mysqld]字段下添加参数

 skip-grant service mysqld restart      重启数据库服务

use mysql

update user set password=password('aming.com') where user='root';

select * from user where user='root'\G;
 /etc/init.d/mysqld  restart

 mysql登陆

 

mysql -uroot -h127.0.0.1 -P3306 -paming.com

grant all on *.* to 'root'@'192.168.1.111' identified by '123aaa';

use mysql

select * from user where host='192.168.1.111';

select * from user where host='192.168.1.111'\G;

 mysql -uroot -S /tmp/mysql.sock -p

 mysql常用操作-1  库 -表-行 - 字段

show databases 

 查看某个库的表

 

> use db; show tables; 查看表的字段

> desc tb; 查看建表语句

> show create table tb;                          当前是哪个用户

> select user();                                        查看当前库

 > use db; show tables                           查看某个库的表

> show create table tb;                           查看建表语句

> select database();                               创建库

> create database db1;                         创建表

> create table t1 (`id` int(4), `name` char(40)); 插入数据

> select version(); 查看数据库版本

> show status; 查看mysql状态

create database aming;

use aming;

show create table tb1\G;

create table t1 (`id` int(4), `name` char(40)); ENGINE=MYISAM DEFAULT CHARSET=gbk;

show tales;

desc tb1;

insert into tb1 valuse(1, 'aming');

select * from tb1;

insert into tb1 (`id`) valuse(1);

insert into tb1 (`id`) valuse(4);

insert into tb1 (`name`,`id`) valuse('55',6);

delete from tb1 where name='55';

truncate table aming.tb1;

drop database aming;

  mysql常用操作-2

 

grant all ondiscuz.* to 'user1'@'*' identified by 'aaabbbcccddd=ipo9';       创建普通用户并授权

 > select count(*) from mysql.user;                                   查询

 > select * from mysql.db; select * from mysql.db where host like '10.0.%';            查询

> UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user='username  更改密码

show proccesslist

set global max_connections=200;

show variables like '%max_connex%';

show status;

vim /etc/init.d/mysqld

cd /data/mysql

ls

tail bogon.err

repair table discuz.zip_forum_post;   修复表

 

mysql备份与恢复

mysqldump -uroot -paming.com discuz

  mysqldump -uroot -paming.com discuz > /data/discuz.sql   备份

cd /data/mysql/

ls

cd discuz/
rm -rf pre_forum_post*

 mysql -uroot -paming.com discuz < /data/discuz.sql  恢复

msqldump -uroot -paming.com discuz pre_forum_post > /data/discuz.sql

cat !$

!rm

!/et

msql -uroot -paming.com discuz  < /data/discuz.sql

msqldump-uroot --default-character-set=gbk -paming.com discuz pre_forum_post > /data/discuz.sql

msql -uroot --default-character-set=gbk -paming.com discuz pre_forum_post > /data/discuz.sql

你可能感兴趣的:(LAMP架构搭建与优化)