前言
本文主要讲解CentOS8怎样部署LNMP环境,在之前安装dovker-ce时讲到了CentOS8的新特性,所以在此就不怎么过多介绍哈!
目录一、优化CentOS8二、部署 LNMP前提准备三、源码编译安装nginx -1.8.0.tar.gz 一、优化CentOS8既然要使用CentOS8,那么我们就不先优化CentOS8才行。如下:
1、替换CentOS镜像
[14:21:01 root@wangzhike yum.repos.d]# dnf install net-tools -y
//安装网络插件
[14:23:44 root@wangzhike yum.repos.d]#cp CentOS-Base.repo CentOS-Base.repo.bak
[14:24:00 root@wangzhike yum.repos.d]#cp CentOS-AppStream.repo CentOS-AppStream.repo.bak
[14:24:00 root@wangzhike yum.repos.d]#cp CentOS-Extras.repo CentOS-Extras.repo.bak
//备份CentOS仓库
[14:25:27 root@wangzhike yum.repos.d]#sed -i 's/mirrorlist=/#mirrorlist=/g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo
[14:25:48 root@wangzhike yum.repos.d]#sed -i 's/#baseurl=/baseurl=/g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo
[14:26:05 root@wangzhike yum.repos.d]# sed -i 's/http:\/\/mirror.centos.org/https:\/\/mirrors.aliyun.com/g' CentOS-Base.repo CentOS-AppStream.repo CentOS-Extras.repo
//更改为阿里云
[14:27:15 root@wangzhike yum.repos.d]#dnf clean all //清除缓存
[14:27:43 root@wangzhike yum.repos.d]# dnf makecache //构建元数据
[14:28:11 root@wangzhike yum.repos.d]# dnf -y install epel-release //red-hat扩展源
2、直接使用阿里云官网推荐
直接进阿里云网站https://developer.aliyun.com/mirror/centosspm=a2c6h.13651102.0.0.3e221b11MV0J8e
直接下载使用
1)、备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2)、下载新的 CentOS-Base.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
3)、运行 yum makecache 生成缓存
二、部署 LNMP前提准备1、创建目录上传源码包
[14:34:05 root@wangzhike ~]#mkdir /software
[14:34:35 root@wangzhike ~]#cd /software/
[14:37:03 root@wangzhike software]#ls
libmcrypt-2.5.8.tar.bz2 nginx-1.8.0.tar.gz php-5.6.13.tar.bz2
mysql-8.0.19.tar.gz pcre-8.40.tar.bz2
2、关闭防火墙
[14:37:04 root@wangzhike software]#systemctl stop firewalld //关闭
[14:39:02 root@wangzhike software]#systemctl disable firewalld //防止开机启动
3、安装·开发包组和依赖包
[14:39:07 root@wangzhike software]#yum groupinstall "Development Tools" "Development Libraries" –y
[14:40:26 root@wangzhike software]#yum install gcc gcc-c++ autoconf automake libtool make zlib zlib-devel openssl openssl-devel pcre* pcre-devel -y
4、源码安装pcre-8.40.tar.bz2
[14:41:07 root@wangzhike software]#tar xf pcre-8.40.tar.bz2 -C /usr/local/src/
//解压此安装包即可,不需要安装
三、源码编译安装nginx -1.8.0.tar.gz
1、安装
1)、解压至指定目录
[14:43:46 root@wangzhike software]#tar xf nginx-1.8.0.tar.gz -C /usr/local/src/
[14:44:08 root@wangzhike software]#cd /usr/local/src/nginx-1.8.0/
2)、编译
[14:45:48 root@wangzhike nginx-1.8.0]#./configure \
--prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.40
//注意--with-pcre=/usr/local/src/pcre-8.40不要写错了
[14:48:07 root@wangzhike ~]# make -j 3 ; make install ; cd
执行这步会报错
错误1
make[1]: *** [objs/Makefile:461: objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/usr/local/src/nginx-1.8.0'
make: *** [Makefile:12: install] Error 2
解决:
[14:49:52 root@wangzhike nginx-1.8.0]#vim objs/Makefile
去掉-Werror,重新编译即可
错误2
make[1]: *** [objs/Makefile:727: objs/src/os/unix/ngx_user.o] Error 1
编辑文件ngx_user.c //注意:是在编译源码当前目录[15:12:25 root@wangzhike nginx-1.8.0]#执行
[15:12:25 root@wangzhike nginx-1.8.0]#cd src/os/unix
[15:13:42 root@wangzhike unix]#vi ngx_user.c
改成这样保存退出:
重新执行make -j 3 ;make install ;cd
出现这样的画面就没有问题
3)、创建运行nginx用户
[15:23:52 root@wangzhike ~]#useradd -M -u 8001 -s /sbin/nologin nginx
[15:24:14 root@wangzhike ~]#id nginx
uid=8001(nginx) gid=8001(nginx) groups=8001(nginx)
2、Nginx目录结构
[15:25:12 root@wangzhike ~]#ll /usr/local/nginx/
total 0
drwxr-xr-x 2 root root 333 Apr 12 15:20 conf #Nginx相关配置文件
drwxr-xr-x 2 root root 40 Apr 12 15:20 html #网站根目录
drwxr-xr-x 2 root root 6 Apr 12 15:20 logs #日志文件
drwxr-xr-x 2 root root 19 Apr 12 15:20 sbin #Nginx启动脚本
3、配置Nginx支持php文件
[15:25:47 root@wangzhike ~]#vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
user nginx nginx; //添加这一行
找到下面内容添加index.php
找到下面这段内容,将这段内容复制,去掉#且修改为如下
4、启动Nginx
[15:34:27 root@wangzhike ~]# /usr/local/nginx/sbin/nginx #启动
[15:37:40 root@wangzhike ~]#netstat -tlnp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 127128/nginx: maste
[15:37:55 root@wangzhike ~]# ps aux|grep nginx
root 127128 0.0 0.0 35412 584 ? Ss 15:37 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 127129 0.0 0.2 68416 3980 ? S 15:37 0:00 nginx: worker process
root 128289 0.0 0.0 9180 1060 pts/5 S+ 15:38 0:00 grep --color=auto nginx
5、Nginx维护命令
1)、检查配置文件语法是否有错误
[15:38:39 root@wangzhike ~]#/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2)、重新加载配置文而建(平滑重启)
[15:41:09 root@wangzhike ~]#/usr/local/nginx/sbin/nginx -s reload
3)、添加环境变量
[15:41:48 root@wangzhike ~]#vim /etc/profile.d/nginx.sh
[15:43:17 root@wangzhike ~]#source /etc/profile.d/nginx.sh
4)、设置开机启动
[15:43:39 root@wangzhike ~]#echo "/usr/local/nginx/sbin/nginx &" >> /etc/rc.local
使用浏览器测试 http://IP地址
到这里,CentOS8部署LNMP环境之编译安装nginx成功!其实觉得与CentOS7部署LNMP差不多,只不过多了些些依赖,报错等等
之后再续CentOS8部署LNMP之编译安装mysql