CentOS 7下的Nginx安装方法

Nginx安装

两种安装方法。 yum安装: 地址:http://nginx.org/en/linux_packages.html#RHEL

1.1 配置官方源yum安装

[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web01 ~]$yum -y install nginx
[root@web01 ~]$rpm -qa nginx
nginx-1.22.1-1.el7.ngx.x86_64
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
[root@web01 ~]# systemctl status nginx
[root@web01 ~]$netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1674/nginx: master  

浏览器打开测试http://10.0.0.7/出现以下内容代表成功。

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

1.2 编译安装

下载:

mkdir -p /server/tools
cd /server/tools
wget http://nginx.org/download/nginx-1.22.1.tar.gz

安装依赖。

#rewrite正则相关的pcre:URL重写软件,实现伪静态跳转、SEO优化等。
yum install pcre pcre-devel -y
#https加密用。
yum install openssl openssl-devel -y
rpm -qa pcre pcre-devel openssl openssl-devel

编译安装步骤

userdel -r www
useradd -u 1111 -s /sbin/nologin www -M
id www
tar xf nginx-1.22.1.tar.gz
cd nginx-1.22.1/
./configure  --user=www --group=www --prefix=/application/nginx-1.22.1/ --with-http_stub_status_module  --with-http_ssl_module --with-pcre --with-stream
make 
make install
ln -s /application/nginx-1.22.1/ /application/nginx
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx
netstat -lntup|grep nginx

配置环境变量

[root@web02 nginx-1.22.1]$vi /etc/profile
export PATH="/application/nginx/sbin:$PATH"
[root@web02 nginx-1.22.1]$. /etc/profile
[root@web02 nginx-1.22.1]$echo $PATH
/application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

配置systemctl启动方式

vi /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=/application/nginx/logs/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat $PIDFile)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat $PIDFile)"

[Install]
WantedBy=multi-user.target

检查

[root@web02 ~]$systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web02 ~]$systemctl start nginx
[root@web02 ~]$ps -ef|grep nginx
root       7577      1  0 07:45 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
www        7578   7577  0 07:45 ?        00:00:00 nginx: worker process
root       7580   1473  0 07:45 pts/1    00:00:00 grep --color=auto nginx
[root@web02 ~]$lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   7577 root    6u  IPv4  38673      0t0  TCP *:http (LISTEN)
nginx   7578  www    6u  IPv4  38673      0t0  TCP *:http (LISTEN)
[root@web02 ~]$systemctl stop nginx
[root@web02 ~]$lsof -i :80
[root@web02 ~]$ps -ef|grep nginx
root       7595   1473  0 07:45 pts/1    00:00:00 grep --color=auto nginx

标签:

上一篇Ansible - Ad-Hoc命令行学习
下一篇Nginx 基于域名的虚拟主机实践

相关文章