实验环境

  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. python 对Excel表格的写入

    python对Excel表格写入需要导入xlrd ,和xlutils两个库 from xlrd import open_workbook from xlutils.copy import copy o ...

  2. django信号

    什么是信号? 信号是在某个操作前或后自动触发一些操作. 信号是通知,是一种状态,相当于在某种状态下发特定的消息 --为了实现代码层解耦 村长博客:http://www.cnblogs.com/legu ...

  3. mysql更新字段内容

    update article set a_content = REPLACE(`a_content`,'www.abc.com','www.bcd.com')

  4. Snakes 的 Naïve Graph

    题解: 首先分析一下这个问题 发现等价于是求n之内与n互素的数的个数,即欧拉函数 这个可以线性筛 但发现还应该减去$x^2==1$的情况 这个东西不是那么好处理 考虑用中国剩余定理拆 因为$p1^{a ...

  5. shiro(四)项目开发中的配置、

    配置拦截.过滤.验证请求 <!-- shiro --> <!-- 項目自定义的Realm --> <bean id="ShiroRealm" clas ...

  6. pygame学习之绘制圆

    import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((600, 500)) ...

  7. swool配置ssl

    1 yum install  openssl  --enable-openssl -y 2 切换在swoole 安装目录 cd /usr/local/swoole 3 ./configure --en ...

  8. SpringBoot使用Elastic-Job

    本文介绍SpringBoot整合Elastic-Job分布式调度任务(简单任务). 1.有关Elastic-Job Elastic-Job是当当网开源的分布式任务调度解决方案,是业内使用较多的分布式调 ...

  9. ISP PIPLINE (十三) CSM/CSC(color space matrix/convert)

    1.RGB为何要转换为YCbCr,历史遗留问题! 一般一个技术如果为了保证原有的设备可以继续使用,就需要兼容以前的技术.黑白电视到彩色电视的进化就是转换为YCbCr的原因,同时YCbCr比RGB传输占 ...

  10. 视频播放—— H5同层播放器接入规范

    H5同层播放器接入规范 x5-video-player-type 启用H5同层播放器 通过video属性“x5-video-player-type”声明启用同层H5播放器 x5-video-playe ...