CentOS7源码安装Nginx
1.准备工作:
wget http://nginx.org/download/nginx-1.8.0.tar.gz
2.安装依赖
#yum install -y make cmake gcc gcc-c++ autoconf automake libpng-devel libjpeg-devel zlib libxml2-devel ncurses-devel bison libtool-ltdl-devel libiconv libmcrypt mhash mcrypt libmcrypt-devel pcre-devel openssl-devel freetype-devel libcurl-devel
3.创建nginx用户
# groupadd -r nginx # useradd -r -g nginx -s /bin/false -M nginx
4.安装Nginx
# cd /usr/local/ # tar -zxvf nginx-1.8.0.tar.gz # cd nginx-1.8.0
5.配置Nginx
# ./configure --prefix=/usr/local/nginx
./configure
过程中,--with-XX_module的表示启用某模块即功能,--without-XX_module表示禁用模块即功能。
编译参数详解链接
6.编译安装
# make # make install
7.启动Nginx
# /usr/local/nginx/sbin/nginx
重载配置文件: # /usr/local/nginx/sbin/nginx –s reload 停止: # /usr/local/nginx/sbin/nginx –s stop 测试配置文件是否正常: # /usr/local/nginx/sbin/nginx –t 强制关闭: # pkill nginx
开机自启
第一步:进入到/lib/systemd/system/目录
# cd /lib/systemd/system/
第二步:创建nginx.service文件
# vim nginx.service
[Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
第三步:加入开机自启动
# systemctl enable nginx
第四步:服务的启动/停止/刷新配置文件/查看状态
# systemctl start nginx.service 启动nginx服务 # systemctl stop nginx.service 停止服务 # systemctl restart nginx.service 重新启动服务 # systemctl list-units --type=service 查看所有已启动的服务 # systemctl status nginx.service 查看服务当前状态 # systemctl enable nginx.service 设置开机自启动 # systemctl disable nginx.service 停止开机自启动
查看Nginx版本号
配置nginx的环境变量
# vim /etc/profile export PATH=$PATH:/usr/local/nginx/sbin
# source /etc/profile
[root@localhost ~]# nginx -v nginx version: nginx/1.15.9 [root@localhost ~]# nginx -V nginx version: nginx/1.15.9 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-threads
检查是否启动成功
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。