美文网首页
Linux学习之路(三) — 搭建nginx服务器

Linux学习之路(三) — 搭建nginx服务器

作者: 饿肚子吃瓜子 | 来源:发表于2019-02-18 17:46 被阅读0次

简介

官网简介:nginx [engine x]是一个 HTTP 和反向代理服务器,一个邮件代理服务器和一个通用的 TCP / UDP 代理服务器,最初由 Igor Sysoev 编写

下载安装 nginx

  1. 安装 nginx 之前需要安装一些依赖包。
# 编译需要依赖 gcc 环境
yum install gcc-c++    

# Perl库,包括 perl 兼容的正则表达式库。
# nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库
# pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
yum install -y pcre pcre-devel    

# zlib 库提供了很多种压缩和解压缩的方式
yum install -y zlib zlib-devel

# 使 nginx 支持 https 协议
yum install -y openssl openssl-devel
  1. 下载 nginx
nginx_download
  • 使用 wget 命令下载
wget -c https://nginx.org/download/nginx-1.14.2.tar.gz
  1. 安装
# 解压安装包。
tar -zxvf nginx-1.10.1.tar.gz
# 进入解压后的文件目录。
cd nginx-1.10.1
# 使用默认配置(也可以自定义配置,不是太懂的最好还是老老实实的使用默认的)
./configure

# 编译安装
make
make install

# 查看 nginx 安装路径
whereis nginx
  1. 配置 nginx.conf

使用包含文件的形式进行配置(推荐)
屏蔽掉 nginx 里面的 server {} 默认配置,在 http {} 中间加一句 include /usr/local/nginx/conf/vhost/*.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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    include /usr/local/nginx/conf/vhost/*.conf;

    # 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;
    #    }
    #}

}

按照上面的路径,新建 vhost 文件夹,在 vhost 文件夹新建 test.apitest.com.conf 文件。
test.apitest.com.conf 文件写入以下的内容,保存并退出。

server {
        listen       80;
        server_name  test.apitest.com;
        charset utf-8;
        root  /var/www/html/project;

        location / {
            index index.php index.html index.htm ;
            # include /usr/local/nginx/conf/rewrite/apitest.conf;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param  PATH_INFO      $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}
  1. nginx 的一些常规命令(比较麻烦,下面有变简单的方法)
cd /usr/local/nginx/sbin/

./nginx -t    # 检查 nginx 配置文件

./nginx -s start    # 启动

./nginx -s stop    # 停止,也可以使用 ./nginx -s quit

./nginx -s reload    # 重启。

添加 nginx 到系统服务

  1. 创建 nginx 启动命令脚本
vi /etc/init.d/nginx
  1. 插入以下内容,注意修改PATH和NAME字段,匹配自己的安装路径
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
  1. 设置执行权限
chmod a+x /etc/init.d/nginx
  1. 注册成服务
chkconfig --add nginx
  1. 设置开机启动
chkconfig nginx on
  1. 重启虚拟机,查看 nginx 是否自动运行。
shutdown -h 0 -r
netstat -apn|grep nginx
  1. 加入系统服务后的 nginx 启动/关闭/重启 命令
systemctl start nginx    # 启动 nginx 

systemctl stop nginx    # 停止 nginx

systemctl restart nginx    # 重启 nginx

systemctl reload nginx    # 重新读取nginx的配置(这个最常用, 不用停止 nginx 服务就能使修改的配置生效)
  1. 但这个时候你会发现 nginx -t 还是不能使用,原因是因为没有将 nginx 加入环境变量。
# 打开环境变量文件。
vi /etc/profile

# 在 export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL 之前添加以下内容。
NGINX_PATH='/usr/local/nginx'
export PATH=$PATH:$NGINX_PATH/sbin

保存退出,执行 source /etc/profile 使其配置生效。
这样就可以在任何地方使用 nginx 了。

参考

相关文章

网友评论

      本文标题:Linux学习之路(三) — 搭建nginx服务器

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