Nginx是一款高性能的Web服务器,可以作为反向代理服务器或负载均衡服务器使用。在Linux系统中,可以通过包管理器安装Nginx,也可以使用编译安装的方式安装Nginx。下面是关于编译安装Nginx的相关信息。
- yum 安装
- 源码编译安装
编译安装是指用户自己下载软件源代码,然后自己编译、配置、安装的安装方式。
编译安装的优点是可以自定义编译选项,可以实现对软件的个性化定制,而缺点是安装过程相对复杂,需要手动编译、配置、安装,并且需要自己处理依赖关系。
ninx官网地址:http://nginx.org/
ninx中文地址:http://nginx.p2hp.com/index.html
一、环境准备
1.查看操作系统信息
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#
2.禁用selinux和firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]#systemctl stop firewalld
3.配置阿里yum源
# 1.下载安装wget
yum install -y wget
# 2.备份默认的yum
[root@localhost yum.repos.d]# mkdir /etc/yum.repos.d/bak
[root@localhost yum.repos.d]# mv /etc/yum.repos.d/
bak/ CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
[root@localhost yum.repos.d]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
[root@localhost yum.repos.d]# ll
total 4
drwxr-xr-x. 2 root root 4096 Oct 23 11:28 bak
[root@localhost yum.repos.d]#
# 4.下载阿里yum配置到该目录中,选择对应版本
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 5.更新epel源为阿里云epel源
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 6.重建缓存
yum clean all
yum makecache
# 7.看一下yum仓库有多少包
yum repolist
4.安装必要编译开发环境包
##必要软件
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel autoconf automake make
##其他
yum install openssl openssl-devel wget httpd-tools vim -y
4.下载nginx 1.24的源码包
编译安装主要用到的选项,示例如下
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
--prefix=/usr/local/nginx #指定安装地址
--with-http_ssl_module #ssl模块,支持https
--with-http_stub_status_module #监控nginx的整体访问情况
--with-threads #多线程,添加线程池机制
--with-file-aio #aio就是异步I/O,允许进程进行不受阻塞或不需要等待I/O完成的I/O操作
[root@localhost opt]# mkdir nginx
[root@localhost opt]# cd nginx/
[root@localhost nginx]# wget http://nginx.p2hp.com/download/nginx-1.24.0.tar.gz
5.解压,查看编译配置项
[root@localhost nginx]# tar -zxvf nginx-1.24.0.tar.gz
[root@localhost nginx]# ll
total 1092
drwxr-xr-x. 8 1001 1001 4096 Apr 11 2023 nginx-1.24.0
-rw-r--r--. 1 root root 1112471 Apr 12 2023 nginx-1.24.0.tar.gz
##查看配置项
[root@localhost nginx-1.24.0]# ./configure --help
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
--user=USER set non-privileged user for
worker processes
--group=GROUP set non-privileged group for
worker processes
--build=NAME set build name
--builddir=DIR set build directory
--with-select_module enable select module
--without-select_module disable select module
--with-poll_module enable poll module
--without-poll_module disable poll module
--with-threads enable thread pool support
--with-file-aio enable file AIO support
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_slice_module enable ngx_http_slice_module
--with-http_stub_status_module enable ngx_http_stub_status_module
--without-http_charset_module disable ngx_http_charset_module
--without-http_gzip_module disable ngx_http_gzip_module
--without-http_ssi_module disable ngx_http_ssi_module
--without-http_userid_module disable ngx_http_userid_module
--without-http_access_module disable ngx_http_access_module
--without-http_auth_basic_module disable ngx_http_auth_basic_module
--without-http_mirror_module disable ngx_http_mirror_module
--without-http_autoindex_module disable ngx_http_autoindex_module
--without-http_geo_module disable ngx_http_geo_module
--without-http_map_module disable ngx_http_map_module
--without-http_split_clients_module disable ngx_http_split_clients_module
--without-http_referer_module disable ngx_http_referer_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--without-http_proxy_module disable ngx_http_proxy_module
--without-http_fastcgi_module disable ngx_http_fastcgi_module
--without-http_uwsgi_module disable ngx_http_uwsgi_module
--without-http_scgi_module disable ngx_http_scgi_module
--without-http_grpc_module disable ngx_http_grpc_module
--without-http_memcached_module disable ngx_http_memcached_module
--without-http_limit_conn_module disable ngx_http_limit_conn_module
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
--without-http_upstream_hash_module
disable ngx_http_upstream_hash_module
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
disable ngx_http_upstream_least_conn_module
--without-http_upstream_random_module
disable ngx_http_upstream_random_module
--without-http_upstream_keepalive_module
disable ngx_http_upstream_keepalive_module
--without-http_upstream_zone_module
disable ngx_http_upstream_zone_module
--with-http_perl_module enable ngx_http_perl_module
--with-http_perl_module=dynamic enable dynamic ngx_http_perl_module
--with-perl_modules_path=PATH set Perl modules path
--with-perl=PATH set perl binary pathname
--http-log-path=PATH set http access log pathname
--http-client-body-temp-path=PATH set path to store
http client request body temporary files
--http-proxy-temp-path=PATH set path to store
http proxy temporary files
--http-fastcgi-temp-path=PATH set path to store
http fastcgi temporary files
--http-uwsgi-temp-path=PATH set path to store
http uwsgi temporary files
--http-scgi-temp-path=PATH set path to store
http scgi temporary files
--without-http disable HTTP server
--without-http-cache disable HTTP cache
--with-mail enable POP3/IMAP4/SMTP proxy module
--with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module
--with-mail_ssl_module enable ngx_mail_ssl_module
--without-mail_pop3_module disable ngx_mail_pop3_module
--without-mail_imap_module disable ngx_mail_imap_module
--without-mail_smtp_module disable ngx_mail_smtp_module
--with-stream enable TCP/UDP proxy module
--with-stream=dynamic enable dynamic TCP/UDP proxy module
--with-stream_ssl_module enable ngx_stream_ssl_module
--with-stream_realip_module enable ngx_stream_realip_module
--with-stream_geoip_module enable ngx_stream_geoip_module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
--without-stream_limit_conn_module disable ngx_stream_limit_conn_module
--without-stream_access_module disable ngx_stream_access_module
--without-stream_geo_module disable ngx_stream_geo_module
--without-stream_map_module disable ngx_stream_map_module
--without-stream_split_clients_module
disable ngx_stream_split_clients_module
--without-stream_return_module disable ngx_stream_return_module
--without-stream_set_module disable ngx_stream_set_module
--without-stream_upstream_hash_module
disable ngx_stream_upstream_hash_module
--without-stream_upstream_least_conn_module
disable ngx_stream_upstream_least_conn_module
--without-stream_upstream_random_module
disable ngx_stream_upstream_random_module
--without-stream_upstream_zone_module
disable ngx_stream_upstream_zone_module
--with-google_perftools_module enable ngx_google_perftools_module
--with-cpp_test_module enable ngx_cpp_test_module
--add-module=PATH enable external module
--add-dynamic-module=PATH enable dynamic external module
--with-compat dynamic modules compatibility
--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid values:
pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, sparc32, sparc64, ppc64
--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation support
--without-pcre2 do not use PCRE2 library
--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro
--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sources
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
--with-debug enable debug logging
6.开始配置nginx选项
./configure --prefix=/usr/local/opnginx
7.编译和安装
##编译
make
##安装
make install
##可以直接
make && make install
安装成功后,可以直接启动nginx测试。
8.创建非root用户启动nginx
nginx默认监听端口是80, 默认情况下linux是不支持非root账户使用1024下端口
8.1创建nginx用户
##创建非登录用户
useradd -r -s /usr/sbin/nologin nginx
##文件授权
chown -R nginx:nginx /usr/local/nginx
8.2启动nginx,报错,非root不能监听80端口
[root@localhost sbin]# sudo -u nginx /usr/local/opnginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
[root@localhost sbin]#
解决办法:
setcap cap_net_bind_service=+eip /usr/local/nginx/sbin/nginx
9.卸载nginx
直接删除安装的目录和用户即可。
10.nginx更新
先关闭nginx
sudo -u nginx /usr/local/nginx/sbin/nginx -s stop
#更新配置
sudo -u nginx /usr/local/nginx/sbin/nginx -s reload
nginx的二进制文件放入之前的路径下即可更新/usr/local/nginx/sbin/,nginx的更新只需要替换二进制文件即可
11.将nginx加入环境变量
[root@localhost sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost sbin]# nginx -v
nginx version: nginx/1.24.0
[root@localhost sbin]# which nginx
/usr/local/sbin/nginx
[root@localhost sbin]# cd
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#
12.添加到systemctl服务
每一个 Unit(服务等) 都有一个配置文件,告诉 Systemd 怎么启动这个 Unit 。Systemd 默认从目录/etc/systemd/system/读取配置文件。
但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录。 systemctl enable 命令用于在上面两个目录之间,建立符号链接关系。systemd有系统和用户区分:
系统(/user/lib/systemd/system/)
用户(/etc/lib/systemd/user/)
一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。
/usr/lib/systemd/system
[root@localhost system]# ll nginx.service
-rw-r--r--. 1 root root 505 Oct 23 14:46 nginx.service
[root@localhost system]#
##文件内容如下
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
#ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~
错误1:Failed to stop nginx.service: Unit nginx.service not loaded.
systemctl daemon-reload
##daemon-reload: 重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载。
13.启动nginx和自启nginx
systemctl start nginx
systemctl enable nginx
QAS:报错处理
1.报错:./configure: error: C compiler cc is not found
[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
[root@localhost nginx-1.24.0]#
解决:需要预先安装c编译器gcc
yum install -y gcc
2.报错:/configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决:安装pcre包
yum install -y pcre-devel
3.报错:./configure: error: the HTTP gzip module requires the zlib library.
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决:安装zlib包
yum install -y zlib-devel