nginx安装 配置


本人环境配置

Ubuntu: 17.04
PHP: 7.0.22
  1. 下载安装包

  2. 解压,并按照官方教程安装.

    $ ./configure
    $ make
    $ sudo make install

    需要注意的是,这里如果直接使用./configure,不带任何选项的话,ng服务默认安装在/usr/local/nginx/sbin/nginx中, 在/usr/bin/nginx是不存在的nginx服务的.

$ ./configure
...
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

$ sudo make && sudo make install

$ /usr/bin/nginx
zsh: 没有那个文件或目录: /usr/bin/nginx

如果要自定义nginx服务位置,使用./configure --sbin-path=/usr/bin/nginx即可.

  1. 开启nginx服务,sudo /usr/bin/nginx; 停止ng服务,使用 sudo /usr/bin/nginx -s stop命令; 查看ng进程ID可以使用cat /usr/local/nginx/logs/nginx.pid,或者ps 命令.

    $ sudo /usr/bin/nginx
    $ ps -aux | grep nginx                                                  130 ↵
    root     26889  0.0  0.0  24984   416 ?        Ss   11:07   0:00 nginx: master process /usr/bin/nginx
  2. 安装php-fpm, 配置文件在 /etc/php/7.0/fpm/php.ini,修改配置后可以使用sudo systemctl restart php7.0-fpm重启fpm.

    $ sudo apt-get install php7.0-common php7.0-cli php7.0-fpm
    ...
    NOTICE: Not enabling PHP 7.0 FPM by default.
    NOTICE: To enable PHP 7.0 FPM in Apache2 do:
    NOTICE: a2enmod proxy_fcgi setenvif
    NOTICE: a2enconf php7.0-fpm
    NOTICE: You are seeing this message because you have apache2 package installed.
    Created symlink /etc/systemd/system/multi-user.target.wants/php7.0-fpm.service → /lib/systemd/system/php7.0-fpm.service.
    正在处理用于 systemd (232-21ubuntu5) 的触发器 ...
    正在处理用于 man-db (2.7.6.1-2) 的触发器 ...
    正在处理用于 ureadahead (0.100.0-19) 的触发器 ...
  3. 打开http://localhost/ 即可看到nginx欢迎页. 注意: 如果安装了apache,则需要先关闭apache服务,可以使用sudo service apache2 stop命令关闭.
    nginx欢迎页

  4. 修改nginx.conf配置, 并使用nginx -t检查配置, sudo service nginx reload重启nginx服务.

    $ sudo gedit /usr/local/nginx/conf/nginx.conf
    $ sudo 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
    $ sudo systemctl reload nginx.service

一些问题总结

make[1]: Leaving directory

如果在make或者make install过程中提示make[1]: Leaving directory, 无需担心, 非编译出错问题.

$ sudo make                                                               2 ↵
make -f objs/Makefile
make[1]: Entering directory '/path/to/nginx-1.13.5'
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
    -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
    -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
    -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
    < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory '/path/to/nginx-1.13.5'
$ sudo make install
make -f objs/Makefile install
make[1]: Entering directory '/path/to/nginx-1.13.5'
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
...
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory '/path/to/nginx-1.13.5'

service nginx start无法启动ng

$ sudo service nginx start
Failed to start nginx.service: Unit nginx.service not found.

解决方法: 在/lib/systemd/system目录下,新建nginx.service文件, 文件中添加如下内容:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/nginx -t
ExecStart=/usr/bin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意:PIDFile和nginx服务路径需要填写自己服务器上的相应路径.

执行systemctl daemon-reload重载系统服务管理器.


参考文档


Author: Itaken
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Itaken !
  TOC目录