CentOS7 安装nginx部署vue项目

简单描述:代码开发完了,需要环境来运行测试。服务器上没有nginx,搞起搞起。
  1.  
    在Centos下,yum源不提供nginx的安装,可以通过切换yum源的方法获取安装。也可以通过直接下载安装包的方法,**以下命令均需root权限执行**:
  2.  
    首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)。选定**/usr/local**为安装目录,以下具体版本号根据实际改变。
安装:
1.安装gcc gcc-c++(如新环境,未安装请先安装)
  $ yum install -y gcc gcc-c++
2.安装PCRE库
  1. $ cd /usr/local/
  2. $ wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
  3. $ tar -zxvf pcre-8.33.tar.gz
  4. $ cd pcre-8.33
  5. $ ./configure
  6. $ make && make install
  如报错:configure: error: You need a C++ compiler for C++ support
  解决:yum install -y gcc gcc-c++
3.安装SSL库
  1. $ cd /usr/local/
  2. $ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
  3. $ tar -zxvf openssl-1.0.1j.tar.gz
  4. $ cd openssl-1.0.1j
  5. $ ./config
  6. $ make && make install
4.安装zlib库存
  1. $ cd /usr/local/
  2. $ wget http://zlib.net/zlib-1.2.11.tar.gz
  3. $ tar -zxvf zlib-1.2.11.tar.gz
  4. $ ./configure
  5. $ make && make install
5.安装nginx
  1. $ cd /usr/local/
  2. $ wget http://nginx.org/download/nginx-1.8.0.tar.gz
  3. $ tar -zxvf nginx-1.8.0.tar.gz
  4. $ cd nginx-1.8.0
  5. $ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
  注:--with-http_ssl_module:这个不加后面在nginx.conf配置ssl:on后,启动会报nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 异常

  如报错:./configure: error: SSL modules require the OpenSSL library.
  解决:yum -y install openssl openssl-devel (亲测可用)

  如报错:./configure: error: the HTTP gzip module requires the zlib library
  解决:在–prefix后面接以下命令: --with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源码路径。--with-zlib=/usr/local/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。(我没遇到这个错误)

  
  6. $ make && make install
至此安装完成

启动测试:
  
$ cd /usr/local/nginx/sbin/
  $ ./nginx
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
其他命令:
  
cd /usr/local/nginx/sbin/
  
./nginx 启动
  
./nginx -s stop 停止 相当于先查出nginx进程id再使用kill命令强制杀掉进程
  
./nginx -s quit 停止 相当于是待nginx进程处理任务完毕进行停止
  
./nginx -s reload 重启
  
ps aux|grep nginx 查看nginx进程
  pkill nginx 强制关闭

以上安装方法nginx的配置文件位于:/usr/local/nginx/conf/nginx.conf
Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内,也就是说内层块会自动获取外层块的值作为缺省值。
部署vue项目:
打包之前先把config下index.js中的host改成服务器的ip,其他的跨域配置的ip也要改成服务器的ip(如果后台也是部署在同一台服务器下)
在项目的根路径下,运行命令npm run build 会在根路径下生产dist文件夹,将其中的文件(一般是static文件夹和index.html)通过xftp工具传到 /usr/local/nginx/html下,千万不要把dist这个文件夹也放进去。
/usr/local/nginx/conf/下 修改配置文件nginx.conf 主要是配置端口 代理等信息
cd /usr/local/nginx/sbin/
./nginx
然后在本地浏览器访问服务器 host:port/ 即可

这篇文章绝大部分内容是从下面博客copy过来的,原文中的命令有几处小错误,一处是安装pcre的过程中版本号错误,一处是 ./config要换成./configure这个我忘记是哪个了,看原文的需要注意一下。
原文链接:https://www.cnblogs.com/jackyzm/p/9600738.html






















猜你喜欢

转载自www.cnblogs.com/xuchao0506/p/12166086.html