修改主机名
使用以下命令,修改完后重连即可看到效果:
hostnamectl set-hostname licong
安装 EPEL 源
EPEL 即 Extra Packages for Enterprise Linux,是基于 Fedora 的一个项目,为红帽系的操作系统提供额外的软件包,适用于 RHEL、CentOS 和 Scientific Linux。EPEL 为 CentOS 提供了额外的 1 万多个软件包,而且都不会更新或者替换系统本身组件。
执行下面这条安装命令后,会在 /etc/yum.repos.d 目录下生成一个 epel.repo 文件:
yum -y install epel-release
安装 yum-axelget 插件
yum-axelget 是 EPEL 提供的一个 yum 插件。默认的 yum 是单线程下载的,使用该插件后用 yum 安装软件时可以并行下载。yum-axelget 插件原理是调用系统中的 axel 下载软件,然后根据软件包的大小自动设定线程数。在多线程操作时,还能避免因为线程数过多而导致服务器拒绝下载的问题,大大提高了软件的下载速度,减少了下载的等待时间。注意:通过下面这条安装命令,会同时安装axel下载软件。
yum -y install yum-axelget
更新 CentOS 源
在安装完 EPEL 源和 yum-axelget 插件后,我们就可以利用它们升级当前的 CentOS 了(耗时约 3-5 分钟)。
yum clean all && yum makecache && yum -y update
然后可以使用下面两条命令查看当前 CentOS 的内核版本和发行版本信息。
查看内核版本
cat /proc/version
查看发行版本
yum install redhat-lsb -y
lsb_release -a
依赖库配置,编译和安装 Nginx 1.12.0
先创建名为 nginx
的用户和名为 nginx
的用户组,然后安装 Nginx 所需的依赖库和依赖包,最后通过 .configure
进行安装的详细配置。
#新建 nginx 用户和 nginx 用户组
groupadd -r nginx && useradd -r -g nginx -s /bin/bash -M nginx
# 安装 nginx 必须的依赖库
yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed gcc gcc-c++
# 官网下载 Nginx 1.12.0 的 tar 包,
wget -c http://nginx.org/download/nginx-1.12.0.tar.gz
# 解压 Nginx
tar -zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0
# 下载 pcre 的 tar 包并解压,以便支持 Nginx 的 Rewrite 功能
wget -c https://github.com/SpanishOnion/MyLNMP/raw/master/package/Nginx/pcre-8.36.tar.gz && tar -zxf pcre-8.36.tar.gz
# 下载 zlib 的 tar 包并解压,以便支持 Nginx 的 Gzip 压缩功能
wget -c https://github.com/SpanishOnion/MyLNMP/raw/master/package/Nginx/zlib-1.2.8.tar.gz && tar -zxf zlib-1.2.8.tar.gz
# 新建 Nginx 安装时所需要的目录
cd /var/tmp/ && mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi} && mkdir -p /var/run/nginx
# 进入 Nginx 目录,并配置编译选项
cd ~/nginx-1.12.0 && ./configure
复制以下命令,粘贴并回车,完成对 Nginx 的编译配置:
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_secure_link_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre=pcre-8.36 \
--with-zlib=zlib-1.2.8 \
--with-debug \
--with-file-aio \
--with-mail \
--with-mail_ssl_module \
--http-client-body-temp-path=/var/tmp/nginx/client_body \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-stream \
--with-ld-opt="-Wl,-E"
注意:每行配置末尾以 \ 结束,且末尾不能有空格
以上每个编译选项对应的注释如下表:
配置项 | 释义 |
---|---|
–prefix=/usr/share/nginx \ | Nginx 的 安装目录所在目录 |
–sbin-path=/usr/sbin/nginx \ | Nginx 的 sbin目录所在目录 |
–conf-path=/etc/nginx/nginx.conf \ | Nginx 的 配置文件所在目录 |
–error-log-path=/var/log/nginx/error.log \ | Nginx 的 错误日志所在目录 |
–http-log-path=/var/log/nginx/access.log \ | Nginx 的连接日志所在目录 |
–pid-path=/var/run/nginx/nginx.pid \ | Nginx的进程ID |
–lock-path=/var/lock/nginx.lock \ | |
–user=nginx \ | Nginx所属用户 |
–group=nginx \ | Nginx所属用户组 |
–with-http_ssl_module \ | HTTPS 协议所需 Nginx 的 ssl 模块 |
–with-http_dav_module \ | |
–with-http_flv_module \ | |
–with-http_realip_module \ | |
–with-http_addition_module \ | |
–with-http_xslt_module \ | |
–with-http_stub_status_module \ | |
–with-http_sub_module \ | |
–with-http_random_index_module \ | |
–with-http_degradation_module \ | |
–with-http_secure_link_module \ | |
–with-http_gzip_static_module \ | Nginx的gzip压缩模块 |
–with-http_perl_module \ | |
–with-pcre=pcre-8.36 \ | pcre 的安装目录 |
–with-zlib=zlib-1.2.8 \ | zlib 的安装目录 |
–with-debug \ | 允许DEBUG |
–with-file-aio \ | |
–with-mail \ | |
–with-mail_ssl_module \ | |
–http-client-body-temp-path=/var/tmp/nginx/client_body \ | |
–http-proxy-temp-path=/var/tmp/nginx/proxy \ | |
–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \ | |
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ | |
–http-scgi-temp-path=/var/tmp/nginx/scgi \ | |
–with-stream \ | Nginx1.9.0版本以上特有的stream模块 |
–with-ld-opt="-Wl,-E" | gcc的编译优化 |
等待执行完成后,输入以下命令进行编译安装:
make && make install
配置 Nginx,使之正常工作
成功安装 Nginx 后,我们需要进行一些配置,设置开机启动。
上传 Nginx 服务控制脚本 nginx 至 /etc/init.d/,并赋予执行权限,删除安装包,添加 Nginx 服务到开机启动:
# 移动nginx脚本文件至init.d
mv ~/nginx /etc/init.d/nginx && chmod +x /etc/init.d/nginx
# 删除Nginx安装包
rm -rf nginx-1.12.0*
# 添加nginx服务
chkconfig --add nginx
# 设置开机启动
chkconfig nginx on
# 查看Nginx的进程
ps -ef|grep nginx
# 启动Nginx
service nginx start
# 查看Nginx的详细信息
nginx -V
测试 Nginx 是否能够正常访问
# 查看防火墙运行状态
firewall-cmd --state
# 若防火墙已开启 则运行该命令关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机启动
systemctl disable firewalld.service
# 使用 curl 连接本机地址,测试是否能否访问
curl http://127.0.0.1
最后用浏览器访问公网 IP 地址,如不能显示 Nginx 的欢迎页面,则需要确认服务器的端口是否打开,打开阿里云或腾讯云的控制台,查看服务器安全组中 80 端口是否开启,如未开启,则添加新规则,开启 80 端口即可。
