我一哥们,常以麒麟之才自称,最近告诉我掉了很多头发,说在搭建nginx服务器的时候被难倒了。Nginx是一款高性能的http 服务器,小编今天带你怎样安装nginx,以及怎样配置,最后说一下常见的问题。
(一)nginx需要环境配置
g++环境
yum install gcc-c++
PERE,一个Perl库,包括 perl 兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式:
yum install -y pcre pcre-devel
zlib,zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
openssl,一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议
yum install -y openssl openssl-devel
(二)nginx安装
官网下载地址,http://nginx.org/en/download.html,小编安装的nginx-1.8.1
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
mkdir -p /var/temp/nginx #必须创建一个临时目录,要不会报错
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --with-http_stub_status_module --with-http_ssl_module
make;make install
(三)配置
小编这里给出了自己的亲测可用的配置文件,vim /usr/local/nginx/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 10.168.1.120; #nginx服务器ip,可以ifconfig查看
location / {
root /geneonto/packages/; #需要共享的文件路径
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置完成后需要指定配置文件并且重启nginx服务:
cd /usr/local/nginx/nginx
./nginx -c /usr/local/nginx/nginx/conf/nginx.conf
./nginx -s reload
systemctl stop firewalld #一定要关闭防火墙,要不访问不了
重启并关闭防火墙后,可以上同一个局域网查看并下载文件,地址:http://10.168.1.120
(四)常见问题
1、一定要关闭防火墙
2、重启电脑后需要重新创建临时目录
mkdir -p /var/temp/nginx
3、若出现一下报错,说明端口有被nginx占用,需要杀了重新启动
./nginx -c /usr/local/nginx/nginx/conf/nginx.conf #出现一下报错
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) #说明80端口被占用,需要杀了重新启动nginx
lsof -i :80 #查看占用的程序,结果如下:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4454 root 6u IPv4 16789 0t0 TCP *:http (LISTEN)
nginx 5391 nobody 6u IPv4 16789 0t0 TCP *:http (LISTEN)
kill -9 4454 5391 #杀了这两个进程
重新执行一下命令
./nginx -c /usr/local/nginx/nginx/conf/nginx.conf
./nginx -s reload
网友评论