实验环境

  1. 一台最小化安装的CentOS 7.3 虚拟机

安装nginx

安装nginx依赖包

yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim

Nginx依赖于pcre、zlib、openssl,在编译前配置时如果有问题

可以使用yum方式安装三个包(pcre-devel、zlib-devel、openssl-devel)

从Nginx官网下载Nginx源码包

wget http://nginx.org/download/nginx-1.12.2.tar.gz

解压Nginx源码包到/root/nginx,并查看Nginx源文件结构

tar -xzvf nginx-1.12.2.tar.gz

/root/nginx目录进行编译前配置

cd /root/nginx*
./configure --prefix=/usr/local/nginx --with-http_ssl_module

/root/nginx目录执行编译安装

make && make install

启动nginx

关闭防火墙

setenforce 0
systemctl stop firewalld
systemctl disable firewalld

进入到安装目录/usr/local/nginx,查看目录结构

cd /usr/local/nginx
pwd
ls

启动Nginx

/usr/local/nginx/sbin/nginx

查看Nginx进程是否启动

ps aux | grep nginx

查看Nginx占用的端口号

netstat -tlnp

使用本地主机访问虚拟机上的Nginx服务器

停止nginx

停止Nginx的三种方式

# 1. 立即停止Nginx服务
/usr/local/nginx/sbin/nginx -s stop # 2.完成当前任务后停止
/usr/local/nginx/sbin/nginx -s quit # 3.杀死Nginx进程
killall nginx

把nginx命令添加到环境变量

使用软连接将nginx链接到/usr/local/sbin

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
ll /usr/local/sbin/ | grep "nginx"

显示当前环境变量PATH

echo $PATH

编辑.bash_profile文件

vim ~/.bash_profile

.bash_profile文件末尾加入以下内容

export PATH=$PATH:/usr/local/nginx/sbin

引用.bash_profile文件

source ~/.bash_profile

使用nginx命令

# 启动nginx
nginx
# 停止nginx
nginx -s quit

nginx命令添加到系统服务

创建并编辑文件/root/service-nginx.sh

#!/bin/sh
#
# filename: service-nginx.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="/usr/sbin/nginx"
nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx) #NGINX_CONF_FILE="/etc/nginx/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 -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
} start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
} stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
} restart() {
configtest || return $?
stop
sleep 1
start
} reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
} force_reload() {
restart
} configtest() {
$nginx -t -c $NGINX_CONF_FILE
} rh_status() {
status $prog
} rh_status_q() {
rh_status >/dev/null 2>&1
} case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
# END

/root/service-nginx.sh替换/etc/init.d/nginx

mv /root/service-nginx.sh /etc/init.d/nginx

赋予可执行限权

chmod 755 /etc/init.d/nginx

执行

systemctl start nginx

源码方式安装nginx,自动化安装脚本

#!/bin/bash

# installation configuration
NGINX_VERSION=1.12.2
NGINX_SRC_PATH=/root
NGINX_BIN_PATH=/usr/local/nginx # disable firewall
systemctl stop firewalld
setenforce 0 # installation dependence
yum install -y pcre-devel zlib-devel openssl-devel wget gcc # download nginx source package
cd ${NGINX_SRC_PATH}
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz # unzip source package
tar -xzvf nginx-${NGINX_VERSION}.tar.gz
cd ./nginx-${NGINX_VERSION} # install nginx
./configure --prefix=${NGINX_BIN_PATH} --with-http_ssl_module
make & make install # start nginx service
cd ${NGINX_BIN_PATH}/sbin
./nginx # END

本文链接:https://www.cnblogs.com/connect/p/nginx-install-src.html

Linux下源码安装并配置Nginx的更多相关文章

  1. nginx在Centos7.5下源码安装和配置

    安装nginx 安装nginx依赖包 yum install -y pcre-devel zlib-devel openssl-devel wget gcc tree vim 进入目录/root/se ...

  2. linux下源码安装netcat

    linux下源码安装netcat http://blog.chinaunix.net/uid-20783755-id-4211230.html 1,下载netcat源码,netcat-0.7.1-13 ...

  3. linux下源码安装软件

    在linux下的很多软件都是通过源码包方式发布的,这样做对于最终用户而言,虽然相对于二进制软件包,配置和编译起来繁琐点,但是它的可移植性却好得多,针对不同的体系结构,软件开发者往往仅需发布同一份源码包 ...

  4. Linux 下源码安装大杂烩

    本文仅以记录平常源码安装部分软件是需注意的关键点. 有时为了方便,如在 Ubuntu 系统中,采用 sudo apt-get install soft-version 来安装某一版本的软件显得更为便捷 ...

  5. linux下源码安装apache服务

    1.搭建静态网站是,我们只需要搭建apache服务即可满足要求. 例如:如果我再客户端游览器输入地址,他会找到192.168.1.100这个服务器,然后根据端口会找到apache服务器.apache他 ...

  6. Linux下源码安装MySQL-5.6.25

    从mysql-5.5起,mysql源码安装开始使用cmake了,因此我们得先安装cmake,配置安装目录./configure --perfix=/.....的时候和以前的会有些区别. 一.安装cma ...

  7. Linux下源码安装方式安装MySQL

    1.下载安装包:https://downloads.mysql.com/archives/community/  2.安装开发工具和安装包 因为要把源码编译成二进制数据,所以必须要有编译器和解释器 g ...

  8. Linux下源码安装JDK7

    安装说明 安装环境:Red Hat Enterprise Linux7.1安装方式:源码安装 软件:jdk-7u80-linux-x64.gz 安装 #首先查看系统原有JDK信息 rpm -qa | ...

  9. linux下源码安装rabbitMq

    一.安装erlang前期环境安装1.利用yum安装erlang编译所依赖的环境 yum -y install make gcc gcc-c++ kernel-devel m4ncurses-devel ...

随机推荐

  1. 服务器端 less的安装

    一. 安装 npm apt-get install npm 二. 安装less 在服务器端安装 LESS 的最简单方式就是通过 npm(node 的包管理器), 像这样: $ npm install ...

  2. swoole 4种PHP回调函数风格

    匿名函数 $server->on('Request', function ($req, $resp) use ($a, $b, $c) { echo "hello world" ...

  3. python3 迭代器(Iterator)和生成器(generator)

    一.迭代器定义: 迭代是访问集合元素的一种方式,迭代器是一个可以记住遍历位置的对象: 集合数据类型如list.dict.str等是Iterable但不是Iterator,不过可以通过iter()函数获 ...

  4. Python学习笔记十二

    HTML全称:Hyper Text Markup Language超文本标记语言 不是编程语言 HTML使用标记标签来描述网页 2.  HTML标签 开始标签,结束标签.  例如:<html&g ...

  5. postgre的函数创建

    语法: CREATE [OR REPLACE] FUNCTION function_name (arguments) RETURNS return_datatype AS $variable_name ...

  6. 解决Windows服务无法访问网络映射盘的问题

    下载工具psexec 下载地址:https://docs.microsoft.com/zh-cn/sysinternals/downloads/psexec 百度地址:https://pan.baid ...

  7. 在.Net Framework中调用Python的脚本方法 (以VB和C#为例)

    某个项目中涉及到这样一个情景: VB/C#写的原始项目要调用Python的一些方法完成特殊的操作, 那么这就涉及到了,在.Net Framework中如何调用Python的脚本方法. 具体步骤流程如下 ...

  8. 异步简析之BlockingCollection实现生产消费模式

    目前市面上有诸多的产品实现队列功能,比如Redis.MemCache等... 其实c#中也有一个基础的集合类专门用来实现生产/消费模式 (生产模式还是建议使用Redis等产品) 下面是官方的一些资料和 ...

  9. 022 Jquery总结

    1.大纲 jQuery 库中的 $() 是什么? 网页上有 5 个div元素,如何使用 jQuery来选择它们? jQuery 里的 ID 选择器和 class 选择器有何不同? 如何在点击一个按钮时 ...

  10. 谷歌浏览器F12基本用法

    第一步:打开你想进行调试的页面,并按F12进入到调试模式 此处以百度页面为例进行功能展示 这是关于最右侧“元素选择器”的功能展示 关于第二个功能的使用,这个功能是将页面适应成手机屏幕大小, eleme ...