安装nginx

 
下载Nginx
 
一、安装nginx时必须先安装相应的编译工具
[root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake  
gcc  c语言编译器
gcc-c++ c++语言编译器
autoconf automake  用于make编译的工具
 
[root@xuegod63 ~]#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib  :nginx提供gzip模块,需要zlib库支持
openssl :nginx提供ssl功能
pcre  :支持地址重写rewrite功能
 
安装nginx:
上传nginx软件包对
[root@xuegod63 ~]# yum install lrzsz -y
 
[root@xuegod63 ~]# tar zxvf nginx-1.12.2.tar.gz
[root@xuegod63 ~]# cd nginx-1.12.2
 
[root@xuegod63 nginx-1.12.2]# useradd  -s /sbin/nologin    -M     nginx
#创建一个nginx用户
-s      指定登录shell
-M     不创建家目录
#groupadd -r nginx       -r 系统账号
#useradd  -g nginx -r nginx      
[root@xuegod63 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/bin/nginx   --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module  --with-pcre --with-http_stub_status_module  --with-http_addition_module --with-http_gzip_static_module #检查编译环境
 
参数说明:
--prefix=/usr/local/nginx       指定安装路径
--user=nginx --group=nginx      指定运行nginx进程的用户和组
--with-http_ssl_module          支持ssl加密
--with-http_realip_module       此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用
--with-http_gzip_static_module  这个模块指定压缩
--with-pcre                     此模块支持rewrite功能 
--with-http_stub_status_module  此模块用于查看Nginx的一些状态信息.  
        
[root@xuegod63 nginx-1.12.2]# echo $?
0
 
编译和安装:
[root@xuegod63 nginx-1.12.2]# make -j 4    #编译,把源代码编译成可执行的二进制文件。 
-j 4  #以4个进程同时编译
编译:mysql 内核 1个小时。  -j 4  20分钟。
 
[root@xuegod63 nginx-1.12.2]# make  install   #安装 
 
nginx主要目录结构:
[root@xuegod63 nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin
conf  #配置文件
html  #网站根目录
logs  #日志
sbin  #nginx启动脚本
 
 
启动nginx :
[root@xuegod63 ~]# nginx
查看版本
[root@xuegod63 ~]# nginx -v 
 
查看安装具体信息
[root@xuegod63 ~]# nginx -V   
 
需要重新进行nginx安装:
 [root@xuegod63 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -s stop
[root@xuegod63 nginx-1.12.2]# rm -rf /usr/local/nginx/
 
[root@xuegod63 nginx-1.12.2]# pwd
/root/nginx-1.10.2
[root@xuegod63 nginx-1.12.2]# make clean 
rm -rf Makefile objs
 
编写Nginx的systemd启动脚本
systemd启动脚本不需要编写shell,只需在 /usr/lib/systemd/system/ 目录下建一个 nginx.service 脚本,内容如下
[root@xuegod63 nginx-1.12.2]# pwd
/usr/lib/systemd/system
[root@purenlai system]# cat nginx.service
[Unit]
Description=nginx - high performance web server
#After参数设置项用来确认启动顺序
After=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/bin/nginx -t
ExecStart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
 
[Install]
WantedBy=multi-user.target
 
重新载入所有的[Unit]文件
[root@purenlai system]# systemctl daemon-reload
[root@purenlai system]# systemctl start nginx
[root@purenlai system]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-08-20 14:14:25 CST; 2h 50min ago
  Process: 25287 ExecStart=/usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 25286 ExecStartPre=/usr/bin/nginx -t (code=exited, status=0/SUCCESS)
Main PID: 25290 (nginx)
   CGroup: /system.slice/nginx.service
           ├─25290 nginx: master process /usr/bin/nginx -c /usr/local/nginx/conf/nginx.conf
           └─25291 nginx: worker process
 
Aug 20 14:14:25 purenlai systemd[1]: Starting nginx - high performance web server...
Aug 20 14:14:25 purenlai nginx[25286]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Aug 20 14:14:25 purenlai nginx[25286]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Aug 20 14:14:25 purenlai systemd[1]: Failed to read PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument
Aug 20 14:14:25 purenlai systemd[1]: Started nginx - high performance web server. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2019-08-23 09:30  菩提花开  阅读(196)  评论(0编辑  收藏  举报