在上一篇中介绍了Nginx依赖组件的安装过程:Nginx安装部署(一)
本篇主要内容为Nginx本身在Linux环境下的安装部署步骤。
Nginx下载
访问Nginx官网:http://nginx.org/en,右边栏点击download进入下载页
[root@localhost local]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@localhost local]# tar -xzvf nginx-1.16.0.tar.gz
[root@localhost local]# cd nginx-1.16.0
// --prefix是指定nginx安装的目录,--with-xx指定了pcre和zlib的路径
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx \
> --with-pcre=/usr/local/pcre-8.43 \
> --with-zlib=/usr/local/zlib-1.2.11
Nginx检查环境变量结束后输出以下信息:
Configuration summary
+ using PCRE library: /usr/local/pcre-8.43
+ OpenSSL library is not used
+ using zlib library: /usr/local/zlib-1.2.11
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"
OpenSSL在使用https时才会用到,所以没有指定路径。如果用到openssl,照着例子套就行。
nginx开头的文字建议记住,这些信息告诉了我们Nginx的安装目录,Nginx进程信息,http的访问日志等,使用过程中经常会用到。
// 执行编译和安装
make && make install
cd /usr/local/nginx/sbin
[root@localhost sbin]# ll
总用量 3868
-rwxr-xr-x. 1 root root 3957496 5月 23 22:06 nginx
[root@localhost sbin]# ./nginx
// 查看nginx状态
[root@localhost 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
[root@localhost sbin]# ps -ef | grep nginx
root 90250 1 0 22:07 ? 00:00:00 nginx: master process ./nginx
nobody 90251 90250 0 22:07 ? 00:00:00 nginx: worker process
root 90254 79852 0 22:10 pts/1 00:00:00 grep --color=auto nginx
[root@localhost sbin]# netstat -nlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 90250/nginx: master
可以看到Nginx默认绑定80端口,如果nginx起不来,可以查看日志检查是否端口已绑定。已绑定端口可以执行
service httpd stop
关闭端口。更好的方式是修改nginx的监听端口,Nginx配置文件位于/usr/local/nginx/conf目录下的nginx.conf
vi nginx.conf
将80端口修改成你期望的端口。
重启Nginx
[root@localhost sbin]# ./nginx -s reload
此时access.log文件有字节,开始记录访问日志:
[root@localhost sbin]# cd ../logs/
[root@localhost logs]# ll
总用量 12
-rw-r--r--. 1 root root 423 5月 23 22:17 access.log
-rw-r--r--. 1 root root 331 5月 23 22:17 error.log
-rw-r--r--. 1 root root 6 5月 23 22:07 nginx.pid
[root@localhost logs]# tail -F access.log
192.168.18.1 - - [23/May/2019:22:17:52 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
192.168.18.1 - - [23/May/2019:22:17:53 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.18.129:2345/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
192.168.18.1 - - [23/May/2019:22:18:46 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"