实验环境

  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. Day05(fianl、抽象类、接口)

    final关键字修饰的类不能被继承.修改,修饰的方法不能被子类覆盖,修饰的变量(是常量)不能被修改. 抽象类:abstract关键字定义的类 继承树中越是在上方的类越抽象,在解决实际问题时,通常将父类 ...

  2. 如何设置PDF签名文档,PDF签名文档怎么编辑

    在工作中我们都会遇到有文件需要签名的时候,如果是在身边就直接拿笔来签名了,那么如果没有在身边又是电子文件需要签名的时候应该怎么办呢,这个时候就应该设置一个电子的签名文档,其他的文件电子文件签名很简单, ...

  3. Shell执行*.sql

    > mysql -uroot -p123456 > use db_test > source /root/temp.sql

  4. 如何在MySQL中设置主从复制

    mysql主从同步定义 主从同步机制 配置主从同步 配置主服务器 配置从服务器 使用主从同步来备份 使用mysqldump来备份 备份原始文件 主从同步的小技巧 排错 Slave_IO_Running ...

  5. maven环境搭建及创建maven项目

    Maven项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 1.maven下载地址http://maven.apache.org/download.cgi ...

  6. elsticsearch在kibanna中的操作

    #建立索引 PUT /es_note_tel{ "settings": { "number_of_shards": 1 }, "mappings&qu ...

  7. outlook2013 关闭时最小化到任务栏的完美解决方法

    使用 Keep Outlook Running 加载项 文件->选项->加载项 点击最下面的“转到”按钮 *用管理员身份运行Outlook才可以将 Keep Outlook Running ...

  8. centos6.9关闭防火墙

    /etc/init.d/iptables stop     临时关闭防火墙, chkconfig iptables off    永久关闭防火墙 查看防火墙状态  chkconfig --list i ...

  9. js过滤html标签

    function deleteHtmlTag(str){ str = str.replace(/<[^>]+>|&[^>]+;/g,"").trim ...

  10. SpringBoot配置文件

    一.配置文件 配置文件应该是无论在哪个框架中都是一个重要角色,而我们最为常用的xxx.xml和xxx.properties,还有springboot推荐使用的xxx.yml. 二.SpringBoot ...