
操作系统:Ubuntu 16.04 前提:开启root权限;如果没有,则在操作的时候需要使用sudo去获取一些执行权限。
安装编译器:
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install build-essential
sudo apt-get install libtool安装之前需要提前准备好Nginx的必备软件/库。
这里安装8.44版本。
wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44/
./configure
make
sudo make installwget https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make
sudo make installwget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxvf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g/
./config
make
sudo make install这里安装1.16版本。
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --withhttp_ssl_module --with-http_realip_module --with-http_v2_module --withopenssl=../openssl-1.1.1g
make
make install默认情况下,Nginx被安装在目录/usr/local/nginx中。
cd usr/local/nginx
ls显示:
conf  html  logs  sbin其中Nginx的配置文件存放于conf/nginx.conf,bin文件是位于sbin目录下的nginx文件,logs是存放的启动日志、错误日志、运行日志等。
(1)默认方式启动Nginx服务器(需要sudo权限):
sudo /usr/local/nginx/sbin/nginx这时,会自动读取配置文件:/usr/local/nginx/conf/nginx.conf
打开浏览器访问此机器的IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功:

(2)查看nginx进程:
sudo ps -ef|grep nginx显示:
root      35768      1  0 11:12 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    35769  35768  0 11:12 ?        00:00:00 nginx: worker process
fly       35771   2396  0 11:12 pts/1    00:00:00 grep --color=auto nginx注意,grep --color=auto nginx不是代表nginx启动,前面两行才是。 (3)指定配置文件启动服务器:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf(4)测试配置信息:
sudo /usr/local/nginx/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(5)关于nginx启动出现报错 比如:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()这可能是nginx已经启动了,也可能是80端口被占用了。 可以使用lsof命令查询端口状态:
sudo lsof -i:80COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   35768   root    6u  IPv4  62470      0t0  TCP *:http (LISTEN)
nginx   35769 nobody    6u  IPv4  62470      0t0  TCP *:http (LISTEN)如果是其他进程占用,使用kill命令杀死进程就好,比如
kill -9 35768
kill -9 35769当项目出现报错(比如上传文件),可以通过tail命令查看error.log文件排除问题。
sudo tail -f /usr/local/nginx/logs/error.log# 停 止:
sudo /usr/local/nginx/sbin/nginx -s stop
# 启动:
sudo /usr/local/nginx/sbin/nginx本文提供了一个简洁明了的指南,帮助读者快速上手在Linux环境下安装和配置Nginx。通过以下几个步骤,您将能够轻松地设置一个高效的Web服务器:
通过本文,读者将能够快速掌握在Linux环境下安装和配置Nginx的关键步骤,为搭建高性能的Web服务器打下坚实的基础。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。