跳到主要内容

linux安装nginx

· 阅读需 4 分钟

下载

nginx: download

下载Stable version版本

上传到服务器,并解压

上传到/opt/目录下,执行解压命令

提示

操作时注意版本号

tar -zxvf nginx-1.18.0.tar.gz

安装前确认

在安装nginx前要确认系统中是否安装了必要依赖功能

参考:linux下检查是否安装过某软件包 - 景岳 - 博客园

检查是否安装,依次执行:

yum list installed | grep "gcc"
yum list installed | grep "pcre-devel"
yum list installed | grep "zlib-devel"
yum list installed | grep "openssl-devel"
附 安装命令

gcc:

yum install libgcc

pcre-devel:Linux 安装 pcre - 走看看

yum install -y pcre pcre-devel

zlib-devel: linux centos7下载安装zlib_Silence丶你的名字的博客-CSDN博客_centos7安装zlib

yum install -y zlib zlib-devel

openssl-devel:Linux系统openssl库的安装与报错处理 - 简书

yum install -y openssl-devel

安装 & 配置

# 1.进入解压出来的文件夹内:
cd nginx-1.18.0/
# 2.配置nginx及相关插件(prefix:指定安装位置):
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 3.安装(两个顺序执行):
make && make install
nginx配置文件参考

参考:

user root root;
worker_processes auto; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';

#charset gb2312;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}

}

5.启动准备

# 验证配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 启动
/usr/local/nginx/sbin/nginx
提示

启动完成, 访问一下公网IP地址。

访问不通检查一下服务器安全组和防火墙是否开启80端口;

删除安装包

rm -rf /opt/nginx-1.22.1
附 生命周期命令
/usr/local/nginx/sbin/nginx # 启动nginx(默认加载../conf/nginx.conf)
/usr/local/nginx/sbin/nginx -c /path/nginx.conf # 启动nginx,指定配置文件
/usr/local/nginx/sbin/nginx -t -c /path/nginx.conf # 测试该nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen # 重新打开日志文件
/usr/local/nginx/sbin/nginx -s stop # 停止 Nginx

安装完成

将nginx加入到开机启动

vi /etc/rc.local 在里面添加内容/usr/local/nginx/sbin/nginx