前言

学弟买了bWg的服务器,想搭建个Nginx服务器环境来玩玩,结果直接使用“yum -y install nginx”安装才发现默认yum仓库没有nginx,所以就无法直接安装nginx了,感觉刚开始用Linux的同学都会遇到这样的一些问题,供给参考!

具体安装步骤

Step 1 安装yum源:

yum install libnet libpcap libnet-devel libpcap-devel gc

Step 2 更新yum源:

yum -y upgrade

Step 3 安装Nginx命令:

yum install nginx -y #发现仓库没有nginx

Step 4 增加一个新的yum源:

vi /etc/yum.repos.d/nginx.repo

打开文件复制粘贴即可。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Step 5 检验Nginx的yum源是否有效:

yum list | grep nginx #如果现实Nginx资源列表说明配置操作成功

Step 6 安装Nginx命令:

yum install nginx -y #没有error提示说明没有问题

Step 7 检验是否安装成功:

rpm -q nginx #显示Nginx版本信息,说明安装已经成功

Step 8 去除对 IPv6 地址的监听

vi /etc/nginx/conf.d/default.conf

直接复制粘贴替换即可。

server{
listen80default_server;
#listen[::]:80default_server;
server_name_;
root/usr/share/nginx/html;
#Loadconfigurationfilesforthedefaultserverblock.
include/etc/nginx/default.d/*.conf;
location/{
}
error_page404/404.html;
location=/40x.html{
}
error_page500502503504/50x.html;
location=/50x.html{
}
}

Step 9 重启Nginx服务器:

service nginx restart

Step 10 设置Nginx为自动启动:

chkconfig nginx on

结语

Salamander · 4月18日

可以编译安装写个shell:

#!/bin/sh
set -e # "Exit immediately if a simple command exits with a non-zero status."
basepath=$(cd `dirname $0`; pwd)
DISTRO=''
PM=''
nginx_version='1.10.3'
php_version='7.1.16'
pcre_version='8.38'
zlib_version='1.2.11'
libmcrypt_version='2.5.8'
mhash_version='0.9.9.9'
mcrypt_version='2.6.8'

Get_Dist_Name()
{
    if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
    else
        DISTRO='unknow'
    fi
    echo "Your Linux Distribution is ${DISTRO}";
}

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi

Get_Dist_Name

if [[ $DISTRO == 'CentOS' || $DISTRO == 'RHEL' || $DISTRO == 'Fedora' ]]; then
    yum install -y gcc gcc-c++
    yum install -y libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel mysql-devel
elif [[ $DISTRO == 'Debian' || $DISTRO == 'Ubuntu' ]]; then
    apt-get install -y gcc g++ openssl libssl-dev  libcurl4-openssl-dev \
    libxml2 libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev default-libmysqlclient-dev
fi



# 1. nginx安装
wget http://nginx.org/download/nginx-${nginx_version}.tar.gz
wget https://svwh.dl.sourceforge.net/project/pcre/pcre/${pcre_version}/pcre-${pcre_version}.tar.gz
wget https://versaweb.dl.sourceforge.net/project/libpng/zlib/${zlib_version}/zlib-${zlib_version}.tar.gz
tar -zxvf nginx-${nginx_version}.tar.gz 
tar -zxf pcre-${pcre_version}.tar.gz
tar -zxf zlib-${zlib_version}.tar.gz


# nginx安装 注意 --with-pcre=  --with-zlib --with-openssl  指的是源码路径
cd ./nginx-${nginx_version}
./configure --prefix=/usr/local/nginx-${nginx_version} --with-pcre=./../pcre-${pcre_version}  --with-zlib=./../zlib-${zlib_version} --with-http_stub_status_module \
--with-http_ssl_module
make
make install

echo 'Nginx installed successfully!'

Github

回复

0

感谢,学到了一招,

Aklman 作者 · 4月18日
载入中...