【简单使用】Linux搭建nginx,开启前端服务

最近项目需求,需要自己搭建nginx,配置VUE项目。由于测试环境,一个服务器需要同时搭建多个VUE项目。
环境:Linux

MAC进入linux系统

通过终端登录进入服务器

 ~ ssh 登录名@需要登录的服务器IP地址

安装nginx

Linux 版本的编译和安装:准备工作

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel

官网上提供了Nginx 服务器三种版本的下载,分别是开发版(Mainline version)、稳定版本(Stable version)和过期版本(Legacy versions)。
  页面上下载部分各链接的具体含义:

  • “CHANGES-x.xx”链接,记录的是对应版本的功能变更日志。包括新增功能、功能的优化和功能缺陷的修复等。
  • 紧接着“CHANGES-x.xx”链接后面的“nginx-x.x.x”链接,是 Nginx服务器的 Linux版本下载地址。
  • “pgp”链接,记录的是提供下载的版本使用PGP加密自由软件GnuPG计算后的签名。PGP可以理解为Pretty Good Privacy。这些数据可以用于下载文件的验证。
  • “nginx/Windows-x.x.x”链接,是 Nginx 服务器的Windows版本下载地址。

安装

选择要安装的nginx的文件目录:
安装在:etc目录
下载nginx稳定版本包:

进入目录:cd  /usr/share/
下载包:wget http://nginx.org/download/nginx-1.14.2.tar.gz
解压包:tar xf nginx-1.14.2.tar.gz
移动gz进入nginx-1.14.2:mv nginx-1.14.2 ./nginx-1.14.2/  这一步只是为了以后可以在用,可以将包直接删除掉

打算把nginx安装到路径usr/local
进入文件夹:cd /usr/local/
创建Nginx的安装路径:mkdir nginx
再次进入解压安装包的文件夹,使用以下命令配置并生成Makefile 文件
./configure --prefix=/usr/local/nginx/
编译和安装:make install

部分文件介绍:

  • src 目录存放了 Nginx软件的所有源代码。
  • man 目录中存放了Nginx 软件的帮助文档,Nginx 安装完成后,使用 man 命令可以查看。
  • html 目录中存放了l 两个后缀名为.html的静态网页文件。这两个文件与 Nginx服务器的运行相关。
  • conf 目录存放的是Nginx 服务器的配置文件,包含 Nginx服务器的基本配置文件和对部分特性的配置文件。
  • auto 目录中存放了大量脚本文件,和configure脚本程序有关。
  • configure文件是 Nginx软件的自动脚本程序。运行 configure自动脚本一般会完成两项工作:一是检查环境,根据环境检查结果生成 C 代码;二是生成编译代码需要的Makefile 文件。

启动

进入安装的文件夹:cd /usr/local/nginx/
帮助: ./sbin/nginx -h
开启: ./sbin/nginx 

或许端口被占用,可用命令查: netstat -lnp|grep 80 或者 netstat -ntlp
关闭端口: kill PID

帮助说明:
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /etc/nginx-1.14.2//)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

配置为系统服务

一般情况,安装好 Nginx 后,使用它的命令是 它的路径+对应的命令 ,但路径很长,每次使用都很麻烦,现在添加一个 ** service nginx xxx ** 的方式,简单快捷,在很多教程中也是如此使用。

需要在 /etc/init.d/ 下面创建文件 nginx,把下面的代码考到nginx文件里面
cd /etc/init.d/
touch nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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

注意事项
nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
路径一定对应上

给权限: chmod 755 /etc/init.d/nginx
chkconfig 命令: chkconfig --add nginx
service nginx restart

配置项说明

  1. Nginx 配置文件 server 中指定两个 location 执行,分别为root 和 alias 指令:
location /test/ {
    alias /www/test/;
}
按照上述配置,则访问 /test/ 目录里面的文件时,nginx 会去 /www/test/ 目录找文件

location /test/ {
    root /www/test;
}
按照这种配置,则访问 /test/ 目录下的文件时,nginx 会去 /www/test/test/ 目录下找文件
  1. alias 是一个目录别名的定义,root 则是最上层目录的定义。

  2. 另一个区别是 alias 后面必须要用 “/” 结束,否则会找不到文件,而 root 则对 ”/” 可有可无。

nginx.conf 内容

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

上面的内容对应结构是下图:


20180205152104446.png

启动前端服务

把前端打包好的项目放到目录 /usr/share/nginx-1.14.2/html/
配置页面路径
前后端分离,需要配置跨域
VUE单页项目,通过hash或者histroy这样方式进行跳转页面,所以需要配置重定向跳转index.html

遇到问题

启动的时候找不到 nginx.pid /etc/nginx//logs/nginx.pid

解决方案
./sbin/nginx -c /etc/nginx/conf/nginx.conf

开启https不能启动,找不到ssl模块

需要安装模块

看下编译安装nginx的时候,都编译安装的哪些模块。

1.输入命令

/usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.12.2
configure arguments:

2.下载相应的nginx源码包放在/usr/local/src下解压,进入解压后的目录执行

./configure --with-http_ssl_module  //重新添加这个ssl模块

3.执行以上一条命令出现这个错误(./configure:错误:SSL模块需要OpenSSL库。),原因是因为缺少了OpenSSL,那我们再来安装一个即可执行:

yum -y install openssl openssl-devel

4.执行./configure,再执行

./configure --with-http_ssl_module

5.执行make命令,但是不要执行make install,因为make是用来编译的,而make install是安装,不然你整个nginx会重新覆盖的。

6.在我们执行完做命令后,我们可以查看到在nginx解压目录下,objs文件夹中多了一个nginx的文件,这个就是新版本的程序了。首先我们把之前的nginx先备份一下,然后把新的程序复制过去覆盖之前的即可。

 cp /etc/nginx  /etc/nginx/sbin/nginx.bak
 cp /usr/share/nginx/nginx-1.14.2/objs/nginx  /etc/nginx/sbin/nginx

最后可以启动了。

参考文章

罗小黑战记
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容