总结必要安装的包
- sudo yum groupinstall "Development Tools" -y #httpd的依赖包
- yum install tar -y #tar压缩包
- sudo yum install apr-devel apr-util-devel
- #APR库 提供跨平台接口的库
- sudo yum install pcre pcre-devel # PCRE库和 pcre-config工具--提供PCRE库的编译和链接信息
-
- sudo yum install openssl-devel # Openssl库
-
-
复制代码 方法一、用init.d(这里用HTTPD-2.4.54为例)
1.安装httpd必要的依赖包
- sudo yum groupinstall "Development Tools" -y
复制代码
2.下载httpd2.4 源码
- wget https://archive.apache.org/dist/httpd/httpd-2.4.54.tar.gz
复制代码
3.解压下载压缩包
安装好tar包-----yum install tar -y
- tar -zxvf httpd-2.4.54.tar.gz
- cd httpd-2.4.54
复制代码
4.设置http
- ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
复制代码
源码编译的时候出现这个问题
1、configure: checking for APR... no configure: error: APR not found. Please read the documentation.这个错误,意味着设置脚本无法在系统中找到APR库。APR是一个为操作系统级功能提供跨平台接口的库
- sudo yum install apr-devel apr-util-devel
复制代码 2、 configure: error: pcre(2)-config for libpcre not found. PCRE is required and available from http://pcre.org/ 出现这个问题意味着设置脚本无法在系统中找到PCRE库的 pcre-config 工具。这个工具用于提供PCRE库的编译和链接信息。
- sudo yum install pcre pcre-devel
复制代码 5.编译并安装httpd
- make -j 4
- sudo make install
复制代码 6.创建Apache用户和组
- sudo groupadd -r apache
- sudo useradd -r -g apache -s /sbin/nologin apache
复制代码 7.将 User daemon 改为 User apache:
- sed -r -i "s/^User [a-zA-Z]*/User apache/" /usr/local/httpd/conf/httpd.conf
复制代码
8.将 Group daemon 改为 Group apache
- sed -r -i "s/^Group [a-zA-Z]*/Group apache/" /usr/local/httpd/conf/httpd.conf
复制代码
9.将 DocumentRoot "/usr/local/httpd/htdocs" 改为 DocumentRoot "/var/www":
- sed -r -i "s%^DocumentRoot ".*"%DocumentRoot "/var/www"%" /usr/local/httpd/conf/httpd.conf
复制代码
10.将 <Directory "/usr/local/httpd/htdocs"> 改为 <Directory "/var/www">
- sed -r -i "s%^<Directory ".*htdocs">%<Directory "/var/www">%" /usr/local/httpd/conf/httpd.conf
复制代码
11.确保 /var/www 目录存在
12.创建一个系统服务管理脚本 /etc/init.d/httpd:
- sudo vi /etc/init.d/httpd
复制代码- #!/bin/bash
- . /etc/rc.d/init.d/functions
-
- apachectl=/usr/local/httpd/bin/apachectl
- httpd=/usr/local/httpd/bin/httpd
- prog=httpd
- start() {
- echo -n $"Starting $prog: "
- daemon $httpd $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
- return $RETVAL
- }
-
- stop() {
- echo -n $"Stopping $prog: "
- killproc $httpd
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd
- return $RETVAL
- }
-
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status $httpd
- ;;
- restart)
- stop
- start
- ;;
- condrestart)
- if [ -f /var/lock/subsys/httpd ]; then
- stop
- start
- fi
- ;;
- *)
- echo $"Usage: $0 {start|stop|restart|condrestart|status}"
- exit 1
- esac
-
- exit $?
复制代码 13.赋予执行权限
- sudo chmod +x /etc/init.d/httpd
复制代码 14.将 httpd 服务添加到系统服务管理中,并设置开机启动
- sudo chkconfig --add httpd
- sudo chkconfig httpd on
复制代码 如果出现以下的问题:
查看/etc/init.d/目录下是否存在httpd脚本,并查抄它是否包含chkconfig所需的注释(如# chkconfig: 2345 10 90)-----对于没有包含chkconfig这个就必要以下的操作
添加chkconfig注释:
在脚本的开头部分(通常在#!/bin/sh之后),添加以下注释行
- # chkconfig: 2345 10 90
- # description: Apache HTTP Server
复制代码 15.启动httpd服务
- systemctl start httpd
- systemctl enable httpd
复制代码
16、测试 httpd 服务是否正常运行
利用 curl 命令测试(本地或长途测试 )
步调:
在命令行中输入curl http://<服务器IP地址或域名>。
如果 httpd 服务正常运行,curl命令会返回服务器返回的网页内容,例如 HTML 代码等。如果服务没有正常运行,大概会出现无法连接、超时等错误信息。
我这里用了我服务端的IP地址
方法二:利用 systemd 服务文件(这里用HTTPD-2.4.46为例)
前三个步调与init.d脚本的方法大同小异出现错误的情势也一样可以看以上的方法步调
1.下载源码
wget https://archive.apache.org/dist/httpd/httpd-2.4.46.tar.gz
2.解压源码
tar -xzf httpd-2.4.46.tar.gz
cd httpd-2.4.46
3.编译安装
./configure --prefix=/usr/local/apache --enable-so --enable-ssl
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures遇到这个错误,通常意味着缺少必要的依赖项或环境设置不正确,导致 configure 脚本无法成功构建 SSL 模块
确保你的系统上已经安装了 OpenSSL 开发库
- sudo yum install openssl-devel
复制代码 4.make
出现以下错误,这意味着你的系统中没有安装 make 工具。make 是一个用于管理编译过程的工具,它根据 Makefile 中的指令来编译和链接步伐
- sudo yum groupinstall "Development Tools"
复制代码 5.创建 systemd 服务文件:
创建vim /etc/systemd/system/httpd.service 文件
- [Unit]
- Description=The Apache HTTP Server
- After=network.target
- [Service]
- Type=forking
- PIDFile=/usr/local/apache/logs/httpd.pid
- ExecStart=/usr/local/apache/bin/httpd -k start
- ExecReload=/usr/local/apache/bin/httpd -k restart
- ExecStop=/usr/local/apache/bin/httpd -k stop
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
- ~
复制代码 6.重载systemd设置:
7.启动httpd服务和状态:
- systemctl start httpd
- systemctl status httpd
复制代码
8.测试 httpd 服务是否正常运行(网页访问测试)
条件条件:确保 httpd 服务已经启动,并且防火墙规则允许访问 httpd 服务的端口。
步调:
打开网页浏览器,在地址栏中输入服务器的 IP 地址或者域名(如果有设置域名解析)。
如果 httpd 服务正常运行并且设置正确,应该可以看到 Apache 的默认接待页面或者你本身设置的网站首页。这表明 httpd 服务能够正确地接收和处理惩罚 HTTP 请求,并返回相应的网页内容。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |