美文网首页
【简单使用】Linux搭建nginx,开启前端服务

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

作者: QLing09 | 来源:发表于2019-03-05 11:17 被阅读0次

最近项目需求,需要自己搭建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

最后可以启动了。

参考文章

罗小黑战记

相关文章

网友评论

      本文标题:【简单使用】Linux搭建nginx,开启前端服务

      本文链接:https://www.haomeiwen.com/subject/qnrbuqtx.html