Nginx安装及配置

1、安装编译Nginx依赖包

# yum -y install gcc gcc-c++ make zlib-devel pcre pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed gd-devel

2、官网下载Nginx安装包

 # wget https://nginx.org/download/nginx-1.22.1.tar.gz1

3、创建Nginx运行用户

# useradd -s /sbin/nologin -M nginx

4、解压配置Nginx编译

[[email protected] ~ ]# tar zxvf nginx-1.22.1.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/nginx-1.22.1/
[[email protected] nginx-1.22.1]# ./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--sbin-path=/usr/sbin/nginx \
--error-log-path=/var/log/nginx/nginx_error.log \
--http-log-path=/var/log/nginx/nginx_access.log \
--pid-path=/usr/local/nginx/run/nginx.pid

5、Nginx编译安装

# make && make install

6.测试Nginx是否安装成功

[root@localhost nginx-1.22.1]# nginx -V
nginx version: nginx/1.22.17
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/nginx_error.log --http-log-path=/var/log/nginx/nginx_access.log --pid-path=/usr/local/nginx/run/nginx.pid

7、启动Nginx服务

# /usr/sbin/nginx

8、验证Nginx服务是否启动成功

# netstat -lntp | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      29740/nginx: master

# ps aux |grep nginx

9、系统添加Nginx服务

以systemd形式添加

1.创建 nginx.service文件

[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target


2.[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

2.以systemctl方式启动Nginx

[[email protected] ~]# pkill nginx
[[email protected] ~]# systemctl daemon-reload 
[[email protected] ~]# systemctl start nginx

3.查看 Nginx 服务状态

[[email protected] ~]# ps -ef | grep ngin
root      31469      1  0 23:11 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     31470  31469  0 23:11 ?        00:00:00 nginx: worker process
root      31554   1182  0 23:11 pts/0    00:00:00 grep --

4.验证 Nginx 服务是否成功启动

[[email protected] ~]# netstat -ntlp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      349/nginx: master p 

5.配置 Nginx 服务自动启动

[[email protected] ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

注:一切谨按步骤操作,仅供参考

你可能感兴趣的:(nginx,运维)