Ubuntu16.04下安装nginx(转)

原文链接

1.安装依赖库

  • 1.1.安装gcc g++依赖库
ubuntu平台:

apt-get install build-essential
apt-get install libtool
centeros平台:

centos平台编译环境使用如下指令
安装make:
yum -y install gcc automake autoconf libtool make

安装g++:
yum install gcc gcc-c++
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g-dev
  • 1.4. 安装ssl依赖库
apt-get install openssl

2.安装nginx http://nginx.org

#下载最新版本:
wget http://nginx.org/download/nginx-1.11.3.tar.gz
#解压:
tar -zxvf nginx-1.11.3.tar.gz
#进入解压目录:
cd nginx-1.11.3
#配置:
./configure --prefix=/usr/local/nginx
#编辑nginx:
make
注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
#安装nginx:
sudo make install
#启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx

3.nginx常用命令

  • 3.1启动
/usr/local/nginx/sbin/nginx
./sbin/nginx
  • 3.2停止
./sbin/nginx -s stop
./sbin/nginx -s quit

-s都是采用向 Nginx 发送信号的方式。
  • 3.3重新加载配置
./sbin/nginx -s reload
  • 3.4指定配置文件
./sbin/nginx -c /usr/local/nginx/conf/nginx.conf

-c表示configuration,指定配置文件
  • 3.5查看版本
    方法1:
        ./sbin/nginx -v

        nginx: nginx version: nginx/1.0.0

    方法2:
        poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -V

        nginx: nginx version: nginx/1.0.0
        nginx: built by gcc 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
        nginx: TLS SNI support enabled
        nginx: configure arguments: --with-http_ssl_module --with-openssl=/home/luming/openssl-1.0.0d/
  • 3.6检查配置文件是否正确
poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -t

nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (13: Permission denied)
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2012/01/09 16:45:09 [emerg] 23898#0: open() "/usr/local/nginx/logs/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

如果出现如上的提示信息,表示没有访问错误日志文件和进程,可以sudo(super user do)一下:
poerchant@ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如果显示如上,则表示配置文件正确。否则,会有相关提示。
  • 3.7 显示帮助信息
poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -h
或
poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -?

猜你喜欢

转载自blog.csdn.net/Magic_Engine/article/details/78895828