目录
1. 下载Nginx
1. 安装必要环境
1.1 需要安装gcc环境
yum install gcc-c++
1.2 PERE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
yum install -y pcre pcre-devel
1.3 zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
1.4 openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
2. 安装nginx
2.1 下载和解压
选择stable版本,linux服务器选择nginx-1.22.1
在服务器上用wget直接下载:
cd /usr/local
wget http://nginx.org/download/nginx-1.22.1.tar.gz
解压
tar -xf nginx-1.22.1.tar.gz
2.2 编译
2.2.1 设定配置
进入目录查看配置选项
cd nginx-1.22.1
./configure --help
这里选择如下配置:
./configure \
--prefix=/usr/local/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 \
--with-file-aio \
--with-http_realip_module
创建临时目录
mkdir /var/temp/nginx -p
2.2.2 编译
在nginx-1.22.1目录下,执行make
make
2.2.3 安装
在nginx-1.22.1目录下,执行make install
make install
进入安装目录查看:
cd /usr/local/nginx
ll
其中html是里面首页html文件。conf里面是配置文件。sbin里面只执行文件。
3. 启动nginx
进入sbin,执行命令:./nginx
cd /usr/local/nginx/sbin
./nginx
查看nginx是否启动
ps -aux | grep nginx
4. 配置环境变量
编辑/etc/profile:
vi /etc/profile
新增nginx的路径
export PATH=$PATH:$MAVEN_HOME/bin:/usr/local/nginx/sbin
生效文件
source /etc/profile
5. 加入system管理
执行命令:
vi /usr/lib/systemd/system/nginx.service
输入内容:
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
重启
systemctl daemon-reload
systemctl start nginx