centos7搭建LNMP环境-编译安装&yum安装-超详细

0. 首先建议使用docker搭建环境。链接在这!
1. 首先了解编译安装和yum安装的区别。

windows下安装centos虚拟机,之前写过。传送门~~

编译安装

  1. 可以按需安装,想安在哪个目录下就安在哪个目录下。
  2. 可以设定参数
  3. 可安装自己想要的版本

yum安装:

  1. 安装的方便快捷
  2. 不需要考虑包的依赖
  3. 可以直接使用service nginx start 等命令。
2. yum安装准备工作—切换阿里云yum源
# 1.备份原有的yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
# 2.下载新的CentOS-Base.repo 到/etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 3.清理缓存
yum clean all 
# 4.生成缓存
yum makecache 

如果想查看当前yum源是否切换成功,可随便使用yum安装一个东西即可看到。

yum install vim

base: mirrors.aliyun.com 这一行就代表了yum源地址

3. 编译安装lnmp 很不建议使用编译安装,麻烦且容易报错。
  1. nginx
    1.1 查看本地有没有安装过nginx

    rpm -qa | grep nginx 
    

    如果安装过,则执行如下命令,卸载原有的nginx

    rpm -e nginx-*   
    

    1.2 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装

    yum install gcc-c++
    

    1.3 PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

    yum install -y pcre pcre-devel
    

    1.4 zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

    yum install -y zlib zlib-devel
    

    1.5 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

    yum install -y openssl openssl-devel
    

    1.6 官网下载 tar.gz安装包,地址~
    Mainline version:主线版本
    Stable version:稳定版本
    Legacy versions:旧版本
    在这里插入图片描述

    # 我们lnmp环境全部安装在这个目录下
    cd /usr/local
    # 下载
    wget -c https://nginx.org/download/nginx-1.20.0.tar.gz
    # 解压
    tar -zxvf nginx-1.20.0.tar.gz
    cd nginx-1.20.0
    # 编译
    ./configure --prefix=/usr/local/nginx
    # 安装
    make && make install
    # make[1]: 离开目录“/usr/local/nginx-1.20.0” 最后显示这个是编译安装成功的意思!
    

    启动、停止nginx

    cd /usr/local/nginx/sbin/
    ./nginx 
    ./nginx -s stop
    ./nginx -s quit
    ./nginx -s reload
    # 查看ngin进程
    ps aux | grep nginx
    

    配置nginx的service服务
    首先
    先停掉nginx服务

    killall -9 nginx
    或者使用 cd /usr/local/nginx/sbin/ && ./nginx -s stop

    然后编辑nginx.service配置文件

    vim /etc/init.d/nginx

    在里面写入以下配置:

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    #
    # chkconfig:   - 85 15
    # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /etc/nginx/nginx.conf
    # config:      /etc/sysconfig/nginx
    # pidfile:     /var/run/nginx.pid
    
    # Source function library.
    . /etc/rc.d/init.d/functions
    
    # Source networking configuration.
    . /etc/sysconfig/network
    
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    
    # 配置nginx命令的位置
    # 修改为你的nginx可执行命令的路径如: /usr/local/nginx/sbin/nginx
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
    
    # 指向你的配置文件路径,如:/usr/local/nginx/conf/nginx.conf
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    
    lockfile=/var/lock/subsys/nginx
    
    make_dirs() {
         
         
       # make required directories
       user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
       if [ -n "$user" ]; then
          if [ -z "`grep $user /etc/passwd`" ]; then
             useradd -
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值