其他分享
首页 > 其他分享> > LAMP环境搭建与配置

LAMP环境搭建与配置

作者:互联网

安装LAMP 基础配置解析PHP

安装MYSQL
(1)
下载 cd /usr/local/src
下载源码包 wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
(2)
解压 tar zxf 源码包
(3)安装和配置
建立MySQL用户
#useradd -s /sbin/nologin mysql
创建datadir
#mkdir -p /data/mysql
更改权限
#chown -R mysql:mysql /data/mysql

#[-d /usr/local/mysql ] && mv /usr/local/mysql /usr/local/mysql_old
挪动位置
#mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql

#cd /usr/local/mysql
#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
如果报错需要安装软件包
#yum install -y perl perl-devel autoconf

#cp support-files/my-default.cnf /etc/my.cnf
修改配置文件
#vi /etc/my.cnf
#There are commonly set,remove the # and set as required
basedir = /usr/local/mysql
datadir = /data/mysql/
port = 33066
server_id = 130
socket = /tmp/mysql.sock
复制启动脚本文件
#cp support-files/mysql.server /etc/init.d/mysql
修改启动脚本文件属性
#chmod 755 /etc/init.d/mysql
修改启动脚本
#vi /etc/init.d/mysql
basedir=/usr/local/mysql
datadir=/data/mysql
把MySQL服务加到系统服务列表中
#chkconfig --add mysql
开机就启动
#chkconfig mysql on
启动服务
#service mysql start

#ps -ef |grep mysqld
#yum install net-tools
#netstat -ltunp |grep 3306


安装apache
(1)下载httpd-2.4.46.tar.gz、apr-1.6.5.tar和apr-util-1.6.1.tar
#cd /usr/local/src/
#wget https://mirrors.cnnic.cn/apache/httpd/httpd-2.4.46.tar.gz
#wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.5.tar.gz
#wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
(2)解压
#tar -zxvf httpd-2.4.46.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
如果报错“configure: error: no acceptable C compiler found in $PATH”,则安装软件包
#yum install -y gcc

如果报错“rm: cannot remove ‘libtoolT’: No such file or directory”
#vi configuer
将RM=‘ R M ′ 改 为 R M = ′ RM'改为RM=' RM′改为RM=′RM -f’

#./configure --prefix=/usr/local/apr

#make &&make install
#cd /usr/local/src/apr-util-1.6.1
#yum install expat-devel
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make &&make install
#cd /usr/local/src/httpd-2.4.46
#yum install -y pcre pcre-devel
#./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-mods-shared=most
#make &&make install
如果报错“collect2: error: ld returned 1 exit status make[2]: *** [htpasswd] Error 1”
#cd /usr/local/src/
#cp -r apr-1.6.5 /usr/local/src/httpd-2.4.46/srclib/apr
#cp -r apr-util-1.6.1 /usr/local/src/httpd-2.4.46/srclib/apr-util
如果还是不行,重新执行configure命令,添加一个编译参数:–with-included-apr
再执行make clean all
#make &&make install
#/usr/local/apache2.4/bin/apachectl -M


安装PHP
(1)下载
源码包 php-5.6.30.tar
(2)解压
#cd /usr/local/src
#tar -zxvf php-5.6.30.tar.gz
(3)安装和配置
#yum install -y libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel epel-release libmcrypt-devel libjpeg-turbo libjpeg-turbo-devel
#yum install -y libmcrypt-devel
#cd php-5.6.30
#yum install bzip2-devel.x86_64 -y
#./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
#make && make install


httpd 解析 PHP
#vim /usr/local/apache2.4/conf/httpd.conf
搜索ServerName把ServerName www.example.com:80前#去掉

AllowOverride none
Require all denied

改成:

AllowOverride none
Require all granted

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

DirectoryIndex index.html

改成:

DirectoryIndex index.html index.php

(4)测试安装
检测配置文件是否正确
#/usr/local/apache2.4/bin/apachectl -t
显示Sysntax ok
启动httpd
#/usr/local/apache2.4/bin/apachectl start
查看是否启动
#netstat -lnp |grep httpd
#curl localhost

It works!

测试是否正确解析PHP
#vim /usr/local/apache2.4/htdocs/1.php

<? echo “php解析正确” ?>

#curl localhost/1.php


apache配置

默认虚拟主机
(1)配置
编辑httpd.conf搜索httpd-vhosts,去掉#号

vi /usr/local/apache2.4/conf/httpd.conf

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

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2.4/docs/abc.com"
ServerName abc.com
ServerAlias www.abc.com aaa.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common

<VirtualHost *:80>

DocumentRoot "/usr/local/apache2.4/docs/111.com"
ServerName 111.com
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" common

#cd /use/local/apache2.4/
#mkdir docs
#cd docs
#mkdir abc.com
#vi abc.com/index.html
abc.com
#mkdir 111.com
#vi 111.com/index.html
111.com
#/usr/local/apache2.4/bin/apachectl -t
#/usr/local/apache2.4/bin/apachectl graceful
(3)配置验证
#curl -xlocalhost:80 www.abc.com
abc.com
#curl -xlocalhost:80 www.aaa.com
abc.com
#curl -xlocalhost:80 111.com
111.com

用户认证
(1)配置
#vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2.4/docs/abc.com"
ServerName abc.com
ServerAlias www.abc.com aaa.com
<Directory /usr/local/apache2.4/docs/abc.com>
    AllowOverride AuthConfig
    AuthName "abc.com user auth"
    AuthType Basic
    AuthUserFile /usr/local/apache2.4/docs/.htpasswd
    require valid-user
</Directory>
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common

#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
#/usr/local/apache2.4/bin/htpasswd -cm /usr/local/apache2.4/docs/.htpasswd ytl
#curl -xlocalhost:80 -u ytl:000000 abc.com -I
(2)配置验证
在Windows上访问地址www.testdomain.com,文件在C:\Windows\System32\drivers\etc\hosts。打开文件在最后一行输入:
#你的IP www.testdomain.com
#systemctl stop firewalld
#setenforce 0
域名跳转
(1)配置
设置不是以111.com开头的网站都跳转到111.com上
#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.111.com"
ServerName www.111.com
ServerAlias 111.com
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^111.com$
    RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common

创建文件
#cd /usr/local/apache2.4/docs
#mkdir www.111.com
#cd www.111.com/
#vim index.html
www.111.com
#vim 123.php

<?php echo "www.111.com/123.php" ?>

#/usr/local/apache2.4/bin/apachectl -t
#/usr/local/apache2.4/bin/apachectl graceful
查看是否安装rewrite模块如果没有则还需要一些配置
#/usr/local/apache2.4/bin/apachectl -M |grep rewrite
如果不存在修改配置文件
#vi /usr/local/apache2.4/conf/httpd.conf
去掉LoadModule rewrite_module modules/mod_rewrite.so的注释
#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
#/usr/local/apache2.4/bin/apachectl -M |grep rewrite
(2)配置验证
#curl -xlocalhost:80 2111.com.cn -I
可以看见状态码301,跳转的网址是111.com
访问日志
(1)配置
#vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
ErrorLog “logs/111.com-error_log”
CustomLog “logs/111.com-access_log” common
(2)配置验证
#curl -xlocalhost:80 2111.com.cn -I
#tail /usr/local/apache2.4/logs/111.com-access_log
访问日志不能记录静态文件
(1)配置
#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.111.com"
ServerName www.111.com
ServerAlias 111.com
ErrorLog "logs/111.com-error_log"ii
CustomLog "logs/111.com-access_log" common
上传图片文件到image中 #cd /usr/local/apache2.4/docs/www.111.com #mkdir image #ll image/linux.jpeg 检查配置信息 #/usr/local/apache2.4/bin/apachectl -t Syntax OK #/usr/local/apache2.4/bin/apachectl graceful #curl -xlocalhost:80 www.111.com/image/linux.jpeg -I 查看日志 #tail -f /usr/local/apache2.4/logs/111.com-access_log 打开网页访问111.com/image/linux.jpeg

#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.111.com"
ServerName www.111.com
ServerAlias 111.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
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" combined env=!image-request

保存配置后测试配置文件是否正确没有错误重新加载服务
#/usr/local/apache2.4/bin/apache -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
(2)配置文件
#curl -xlocalhost:80 www.111.com -I
(3)验证成功
#ll /usr/local/apache2.4/logs/


静态元素过期时间
(1)配置
#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.111.com”
ServerName www.111.com
ServerAlias 111.com

<IfModule mod_expires.c>
    ExpiresActive on
    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"
ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" combined

检测配置文件是否正确
#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
(2)配置检验
检查httpd是否加载expires模块
#/usr/local/apache2.4/bin/apachectl -M |grep -i expires
#vim /usr/local/apache2.4/conf/httpd.conf
搜索关键词expires把这一行前面的#删掉
#LoadModule expires_ module modules/mod_ expires .so
重新加载服务
#/usr/local/apache2.4/bin/apachectl graceful
#/usr/local/apache2.4/bin/apachectl -M|grep -i expires
expires_module(share)
测试
#curl -xlocalhost:80 www.111.com/image/linux.jpeg -I


配置防盗链

(1)配置
#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.111.com"
ServerName www.111.com
ServerAlias 111.com

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

  SetEnvIfNoCase Referer "http://www.111.com" local_ref
SetEnvIfNoCase Referer "http://111.com" local_ref
SetEnvIfNoCase Referer "^$" local_ref
<filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
    Order Allow,Deny
    Allow from env=local_ref
</filesmatch>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/111.com-access_log" combined

#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
(2)配置
#curl -e “http://www.douxue.com/123.php” -xlocalhost:80 www.111.com/image/linux.jpg -I
状态码为403
#curl -xlocalhost:80 www.111.com/image/linux.jpg -I
状态码为200


访问控制-Diretory\FileMatch

(1)目录配置
#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.111.com”
ServerName www.111.com
ServerAlias 111.com
<Directory /usr/local/apache2.4/docs/www.111.com/admin>
Order deny,allow
Deny from all
Allow from 127.0.0.1

ErrorLog “logs/111.com-error_log”
CustomLog “logs/111.com-access_log” combined

验证过程
#mkdir /usr/local/apache2.4/docs/www.111.com/admin/
#vi /usr/local/apache2.4/docs/www.111.com/admin/123.php

<?php echo "Hello World!"; ?>

(2)配置验证
#/usr/local/apache2.4/bin/apachectl -t
#/usr/local/apache2.4/bin/apachectl graceful
#curl -x127.0.0.1:80 www.111.com/admin/123.php -I
状态码为200正常访问
#curl -x192.168.63.130:80 www.111.com/admin/123.php -I
状态码为403拒绝访问
(3)针对文件配置
编辑配置文件
#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.111.com”
ServerName www.111.com
ServerAlias 111.com
<Directory /usr/local/apache2.4/docs/www.111.com/>
<FilesMatch "admin.php(.
)">
Order deny,allow
Deny from all
Allow from 127.0.0.1


ErrorLog “logs/111.com-error_log”
CustomLog “logs/111.com-access_log” combined

验证过程:
#cd /usr/local/apache2.4/docs/www.111.com/
#vim admin.php

<?php echo "This is admin.php"; ?>

#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
(4)配置检验
#curl -x192.168.63.130:80 www.111.com/admin.php -I
状态码403被拒绝
#curl -x127.0.0.1:80 www.111.com/admin.php -I
状态码200访问正常


访问控制-禁止解析PHP

(1)配置
#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.111.com”
ServerName www.111.com
ServerAlias 111.com
<Directory /usr/local/apache2.4/docs/www.111.com/upload>
php_admin_flag engine off

ErrorLog “logs/111.com-error_log”
CustomLog “logs/111.com-access_log” combined

#/usr/local/apache2 .4/bin/apachectl -t
Syntax OK
#/usr/local/apache2 .4/bin/apachectl graceful
#cd /usr/local/apache2.4/docs/www.111.com/
#mkdir upload
#vim upload/123.php

<?php echo "www.111.com/123.php"; ?>

(2)配置验证
#curl -x127.0.0.1:80 www.111.com/upload/123.php

<?php echo "www.111.com/123.php"; ?>

访问控制-user_agent

(1)配置
#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.111.com”
ServerName www.111.com
ServerAlias 111.com

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]
    RewriteRule  .*  -  [F]
</IfModule>
ErrorLog "logs/111.com-error_log"
CustomLog "logs/www.111.com-access_log" combined
验证过程: #/usr/local/apache2.4/bin/apachectl -t Syntax OK #/usr/local/apache2.4/bin/apachectl graceful (2)配置验证 #curl -x127.0.0.1:80 www.111.com/upload/123.php 状态码403 打开浏览器访问www.111.com/upload/123.php (3)反向测试 修改配置文件 #/usr/local/apache2.4/bin/apachectl -t Syntax OK #/usr/local/apache2.4/bin/apachectl graceful #curl -x127.0.0.1:80 www.111.com/upload/123.php 浏览器访问 (4)指定客户端 #/usr/local/apache2.4/bin/apachectl -t Syntax OK #/usr/local/apache2.4/bin/apachectl graceful #curl -A Mozllia -x127.0.0.1:80 www.111.com/upload/123.php ****** ## PHP配置 **PHP基础配置** 查看PHP配置文件得位置 #/usr/local/php/bin/php -i |grep -i "loaded configuration file" #cp /usr/local/src/php-5.6.30/php.ini-production /usr/local/php/etc/php.ini #/usr/local/php/bin/php -i |grep -i "loaded configuration file" #cd /usr/local/apache2.4/docs/www.111.com #vim 123.php <?php phpinfo(); ?>

#vim /usr/local/php/etc/php.ini
搜索disable_functions,编辑如下:
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,1eak,popepassthru,stream_socket_server,popen,proc_open,proc_close
#vim /usr/local/php/etc/php.ini
找到date. timezone设置如下:
date.timezone = Asia/Shanghai
日志相关配置
#vim /usr/local/php/etc/php.ini
disable_functions=phpinfo,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,1eak,popepassthru,stream_socket_server,popen,proc_open,proc_close
浏览器访问www.111.com/123.php,显示空白
配置error_log:
#vim /usr/local/php/etc/php. ini
搜索log_errors, 改成如下log_errors=On
搜索error. log,改为error_log = /tmp/php_errors.log
#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
配置完php.ini,查看日志
#tail -f /tmp/php_errors.log
配置open_basedir
#vim /usr/local/php/etc/php.ini
//搜索open_basedir,改成如下
open_basedir = /usr/local/apache2.4/docs/www.111.com/admin:/tmp
#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
打开浏览器依次访问http://www.111.com/admin/123.php、http://www.111.com/upload/123.php
虚拟主机配置
#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.111.com”
ServerName www.111.com
ServerAlias 111.com
php_admin_value open_basedir “/usr/local/apache2.4/docs/www.111.com/admin/:tmp/”
ErrorLog “logs/111.com-error_log”
CustomLog “logs/www.111.com-access_log” combined

#/usr/local/apache2.4/bin/apachectl -t
Syntax OK
#/usr/local/apache2.4/bin/apachectl graceful
#curl -x127.0.0.1:80 www.111.com/admin/123.php
Hello World![root@localhost www.111.com]#
[root@localhost www.111.com]#
#curl -x127.0.0.1:80 www.111.com/upload/123.php -I


PHP扩展模块安装

查看PHP加载了哪些模块
#/usr/local/php/bin/php -m
#cd /usr/local/src
#wget http://pecl.php.net/get/redis-2.2.5.tgz
#tar -zxvf redis-2.2.5.tgz
#mv redis-2.2.5 phpredis-develop
#cd phpredis-develop
#yum install -y autoconf
生成configure文件
#/usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
Installing shared extensions: /usr/local/php/lib/ php/extensions/ no-debug- zts - 20131226/
#ls /usr/local/php/lib/php/extensions/no-debug-zts-20~~
#vim /usr/local/php/etc/php.ini
增加一行配置(可以放在文件最后一行)
extension = redis.so
查看是否加载了redis模块
redis
#/usr/local/php/bin/php -m |grep redis

标签:php,com,配置,www.111,LAMP,usr,apache2.4,local,搭建
来源: https://blog.csdn.net/m0_53112480/article/details/115060312