文章目录
一、Nginx编译安装
1、安装依赖包,gcc-c++编译器,pcre-devel,zlib-devel,openssl-devel
yum -y install gcc-c++ pcre-devel zlib-devel openssl-devel
2、在/usr/local/目录下创建nginx文件夹
mkdir /usr/local/nginx
3、下载nginx安装包
#里面包含所有的nginx版本
https://nginx.org/download/
注意:安装包不要解压到local目录下,否则后面编译安装会报错
cd /usr/local/nginx
wget https://nginx.org/download/nginx-1.1.4.tar.gz
4、解压
tar -xvf nginx-1.1.4.tar.gz
5、执行,使用默认配置
cd /usr/local/nginx/nginx-1.1.4
./configure
6、编译安装
make
make install
7、进入sbin目录,启动nginx
cd /usr/local/nginx/sbin
./nginx
—–nginx已经安装完成,打开浏览器输入IP地址就能访问到Nginx页面
8、把nginx加入到systemctl服务中
创建nginx.service执行文件
vi /usr/lib/systemd/system/nginx.service
输入以下内容
[Unit]
Description=nginx
After=network.target
[Service]
#后台执行模式
Type=forking
#pid存放路径
PIDFile=/usr/local/nginx/logs/nginx.pid
#nginx启动
ExecStart=/usr/local/nginx/sbin/nginx
#nginx重启
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#nginx关闭
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#启动nginx
systemctl start nginx
#重启nginx
systemctl restart nginx
#关闭nginx
systemctl stop nginx
常见问题
1、如果访问不到,先检查看是否有程序占用80端口,杀死占用进程
ps -ef | grep 80
kill -9 pid
1.1、如果端口没有被占用,还是不行,检查防火墙有没有放行80端口
#查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
#开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
1.2或者
#关闭防火墙
systemctl stop firewalld.service
2、执行./configure时报错
原因:没有安装zlib依赖包
yum -y install zlib zlib-devel
二、Apache编译安装
1、安装依赖包
yum -y install gcc-c++ openssl-devel pcre-devel expat-devel libtool
2、下载apr,apr-util,httpd包
cd /usr/local
wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
2.1、解压包
tar xvf apr-1.7.0.tar.gz
tar xvf apr-util-1.6.1.tar.gz
tar xvf httpd-2.4.54.tar.gz
3、安装apr,apr-util,httpd包
#安装apr
[root@Yteach local]# cd apr-1.7.0/
[root@Yteach apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@Yteach local]# make && make install
#安装apr-util
[root@Yteach apr-1.7.0]# cd ..
[root@Yteach local]# cd apr-util-1.6.1/
[root@Yteach apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@Yteach local]# make && make install
#安装httpd
[root@Yteach apr-util-1.6.1]# cd ..
[root@Yteach local]# cd httpd-2.4.53/
[root@Yteach httpd-2.4.53]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
[root@Yteach local]# make && make install
4、创建apache用户和用户组
[root@Yteach htdocs]# useradd -r -M -s /sbin/nologin apache
[root@Yteach htdocs]# id apache
uid=997(apache) gid=995(apache) 组=995(apache)
5、启动apache
●在启动前,先添加防火墙端口,或者关闭防火墙
#开启80端口
[root@Yteach /]firewall-cmd --zone=public --add-port=80/tcp --permanent
#重载防火墙,使配置生效
[root@Yteach /]firewall-cmd --reload
#或者关闭防火墙
systemctl stop firewalld
#绝对路径运行 start|stop|restart
[root@Yteach /]# /usr/local/apache/bin/apachectl -k start
6、将apache加入系统:两种方法
#方法一:
[root@Yteach /]# cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/apache
#在任意路径下都可以执行
[root@Yteach /]# service apache start
[root@Yteach /]# service apache stop
[root@Yteach /]# service apache restart
#方法二:
#配置启动文件
[root@Yteach /]#vim /usr/lib/systemd/system/apache.service
#将以下内容输入apache.service中
[Unit]
Description=apache
After=network.target
[Service]
#后台执行模式
Type=forking
#pid存放路径
PIDFile=/usr/local/apache/logs/httpd.pid
#绝对路径启动
ExecStart=/usr/local/apache/bin/apachectl start
#绝对路径重启
ExecReload=/usr/local/apache/bin/apachectl restart
#绝对路径停止
ExecStop=/usr/local/apache/bin/apachectl stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#赋予文件执行权限
chmod +x /usr/lib/systemd/system/apache.service
启动命令
systemctl start apache #启动
systemctl stop apache #关闭
systemctl restart apache #重启
systemctl enable apache #加入开机自启
systemctl status apache #查看状态
常见问题
1、运行后出现一下问题
[root@Yteach /]# vim /usr/local/apache/conf/httpd.conf
修改ServerName,改为,保存退出
ServerName localhost:80