LAMP安装,apache配置虚拟主机,apache日志轮转

关闭防火墙,selinux

1.mysql安装

见之前mysql安装。

2.apache安装

分为yum安装和源码安装两种方法。

安装前先判断,apache是否已存在。

# rpm -qa | grep httpd
# 根据名字删除包
# rpm -e httpd-tools-2.4...
# 或者
# yum remove httpd

a.yum安装

# 先行绑定yum源,阿里云的就行。
# yum install httpd
# service httpd restart
# 开启个浏览器,输入ip访问,看见apache广告页面即成功。

b.源码安装

需要3个依赖,1个源码包

1.确认gcc组件,openssl

# gcc -v

# 显示这句话判断是否安装
# gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 

# 若没有则
# yum -y groupinstall Development tools
# yum -y install gcc-c++
# yum -y install cmake pcre-devel ncurses-devel;
# yum -y install openssl-devel

2.APR 和 APR-Util下载

下载区地址:http://apr.apache.org/download.cgi

或具体地址:http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2

和:http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2

貌似1.6版本以上会报错

 

# mkdir /software
# cd /software
# wget http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2
# wget http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2

# 解压缩
# tar xf apr-1.5.2.tar.bz2
# tar xf apr-util-1.5.4.tar.bz2

# 分别编译安装
# cd apr-1.5.2
# ./configure
# make && make install

# yum install -y expat-devel*
# cd ..
# cd apr-util-1.5.4
# ./configure --with-apr=/usr/local/apr/bin/apr-1-config
# make && make install

# 若安装apr-util时提示
# xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
# 则补一个
# yum install -y expat-devel*

3.PCRE组件下载

下载区:https://sourceforge.net/projects/pcre/files/pcre/

或具体地址:https://jaist.dl.sourceforge.net/project/pcre/pcre/8.43/pcre-8.43.tar.bz2

# cd /software
# wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.43/pcre-8.43.tar.bz2
# tar xf pcre-8.43.tar.bz2
# cd pcre-8.43
# ./configure
# make && make install

 4.源码包下载

apache下载区 http://httpd.apache.org/download.cgi  或

apache 2.4.41 http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.bz2

# cd /software
# wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.bz2
# tar xf httpd-2.4.41.tar.bz2
# cd /software/httpd-2.4.41
# ./configure --enable-modules=all --enable-mods-shared=all --enable-so --enable-rewrite --with-pcre --enable-ssl --with-mpm=prefork --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr/bin/apu-1-config
# make
# make && install

若提示

configure: WARNING: OpenSSL version is too old
no
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures

# yum -y install openssl-devel

若提示

/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCode'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDeclHandler'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_ParserCreate'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_SetCharacterDataHandler'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_ParserFree'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_SetUserData'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_StopParser'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_Parse'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_ErrorString'
/usr/local/apr/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler'
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/software/httpd-2.4.41/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/software/httpd-2.4.41/support'

 则可能是apr版本高于1.6

3.验证

# ls /usr/local/apache2/

drwxr-xr-x  2 root root  262 Oct 24 11:54 bin
drwxr-xr-x  2 root root  167 Oct 24 11:54 build
drwxr-xr-x  2 root root   78 Oct 24 11:54 cgi-bin
drwxr-xr-x  4 root root   84 Oct 24 11:54 conf
drwxr-xr-x  3 root root 4096 Oct 24 11:54 error
drwxr-sr-x  2 root root   24 Aug  9 21:36 htdocs
drwxr-xr-x  3 root root 8192 Oct 24 11:54 icons
drwxr-xr-x  2 root root 4096 Oct 24 11:54 include
drwxr-xr-x  2 root root    6 Oct 24 11:54 logs
drwxr-xr-x  4 root root   30 Oct 24 11:54 man
drwxr-sr-x 14 root root 8192 Aug  9 21:36 manual
drwxr-xr-x  2 root root 4096 Oct 24 11:54 module
成功

启动apache
# cd /usr/local/apache2/bin/ 
# ./apachectl start

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 
报错信息找不到server name

编辑httpd.conf文件,搜索"#ServerName",添加
# ServerName localhost:80

# ss -nltp
# 查看80端口是否被占用
# curl 10.1.1.10
# 访问ip地址,查看能否访问apache主页。

Apache安装完成。

4.php安装

php下载:

https://www.php.net/downloads.php

http://ftp.ntu.edu.tw/php/distributions/

以 https://www.php.net/distributions/php-7.2.23.tar.bz2 为例

# cd /software
# wget https://www.php.net/distributions/php-7.2.23.tar.bz2
# tar xf php-7.2.23.tar.bz2
# cd php-7.2.23
# ./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysqli --with-pdo-mysql --with-zlib --with-curl --enable-zip --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir --enable-sockets --with-xmlrpc --enable-soap --enable-opcache --enable-mbstring --enable-mbregex --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-calendar --enable-bcmath

# 这些都是要开启的模块,安装完之后要开启可以修改php.ini文件。

错误0
configure:error:jpeglib.h not found 

yum install libjpeg libpng freetype libjpeg-devel libpng-devel freetype-devel -y 

错误1
configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/ 

yum -y install libcurl-devel

错误2
configure: error: libxml2 not found. Please check your libxml2 installation.

yum -y install libxml2-devel libxml2

错误3
checking for cURL 7.10.5 or greater... 
configure: error: cURL version 7.10.5 or later is required to compile php with cURL support

yum -y install curl-devel


显示
Thank you for using PHP.

# 请确保您的机器内存至少512MB!
# make && make install

Build complete.
Don't forget to run 'make test'.
表示成功,不需要make test。

确认php成功安装:
# ls /usr/local/apache2/modules/libphp7.so 
/usr/local/apache2/modules/libphp7.so

5.测试

# vim /usr/local/apache2/conf/httpd.conf
# 解开以下内容的注释

LoadModule negotiation_module modules/mod_negotiation.so 此模块打开注释 
Include conf/extra/httpd-languages.conf 打开此选项,扩展配置文件就生效了

#对虚拟主机的支持,后续再打开 
#Include conf/extra/httpd-vhosts.conf 

LoadModule php7_module modules/libphp7.so 
找到这一句,在这句下面加上两句 添加以下两行意思是以.php结尾的文件都认为是php程序文件。
注意两句话的.php前面都是有一个空格的 
AddHandler php7-script .php AddType text/html .php

默认主页加上index.php,并放在index.html前,支持php的首页文件 
<IfModule dir_module> 
    DirectoryIndex index.php index.html 
</IfModule>


修改apache的子配置文件,优先支持中文 
# vim /usr/local/apache2/conf/extra/httpd-languages.conf 

DefaultLanguage zh-CN 
打开注释,将原来的nl改成zh-CN 

LanguagePriority zh-CN en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv tr zh-TW 
语言集优先集,把zh-CN 写到前面


# cd /usr/local/apache2/htdocs 
# rm -f index.html 
# vim index.php 
将以下内容添加到vim index.php中,最好复制粘贴,不要手敲,避免错误 

<?php phpinfo(); ?>

启动apache

# cp /usr/local/apache2/bin/apachectl /etc/init.d/apache
# netstat -nltp|grep 80 确保没有80端口监听
# service apache start

AH00112: Warning: DocumentRoot [/usr/local/apache2/docs/dummy-host.example.com] does not exist
AH00112: Warning: DocumentRoot [/usr/local/apache2/docs/dummy-host2.example.com] does not exist
这说明你刚刚开启了那个vhosts

httpd (pid 18337) already running
成功

访问IP地址

访问可能提示
You don't have permission to access this resource.

大致修改思路是这样
# vim /usr/local/apache2/conf/httpd.conf

查找<Directory

<Directory "/usr/local/apache2/cgi-bin">
    AllowOverride None
    Options None
    #Require all granted
</Directory>

此处Require all granted就是罪魁祸首,也有可能是别的内容,
比如Deny from all
都注释掉试下。


显示phpinfo信息表示成功。

6.配置虚拟主机

# cd /usr/local/apache2/conf/ 
# vim httpd.conf
打开注释
Include conf/extra/httpd-vhosts.conf

# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
注释掉默认的虚拟主机配置,增加如下内容:

<VirtualHost *:80> 
	ServerAdmin webmaster@dummy-host2.example.com 
	DocumentRoot "/www/myblog" 					#重要,这个是项目路径#
	ServerName www.myblog.io 					#重要,虚拟域名#
	ErrorLog "logs/myblog-error_log" 			#错误日志地址#
	CustomLog "logs/myblog-access_log" common   #访问日志地址#
	<Directory "/www/myblog"> 					#按上面的路径填写#
		Options Indexes FollowSymLinks 			#照抄#
		AllowOverride All 						#是否准许URL重写#
		Require all granted 					#允许所有
		DirectoryIndex index.php 				#默认主页
	</Directory> 
</VirtualHost>

7.验证虚拟主机

# vi /etc/hosts
加入一行
10.1.1.10 www.myblog.io
保存退出

# service network restart
# curl www.myblog.io
# curl 10.1.1.10

一样
成功

Apache日志轮转

核心操作,修改配置文件

1.在httpd.conf中

搜索ErrorLog,CustomLog

替换整行

ErrorLog "| /usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/error_log-%Y%m%d_%H:%M:%S 20"

CustomLog "| /usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/access_log-%Y%m%d_%H:%M:%S 20" common

2.在虚拟主机配置文件中 httpd-vhosts.conf

搜索ErrorLog,CustomLog,修改同上。

注:%YmdHMS是时间参数,自行修改。

修改完毕重启服务(优雅的)

/usr/local/apache2/bin/apachectl graceful

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值