(转)Centos7 Nginx安装

时间:2024-03-23 23:37:02

场景:工作中使用的suse,因为系统可可查资料太少,且系统中一些功能的确实,导致很多集群中功能无法顺利测试通过,在Centos上面进行测试,能够更快的熟悉项目的架构过程!

1 安装准备

首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++、gcc、openssl-devel、pcre-devel和zlib-devel 所以执行如下命令安装

yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel

2 安装Nginx

安装之前,最好检查一下是否已经安装有nginx

find -name nginx  

如果系统已经安装了nginx,那么就先卸载

yum remove nginx

首先进入/usr/local目录

cd /usr/local 

从官网下载最新版的nginx

wget http://nginx.org/download/nginx-1.7.4.tar.gz 

这里也可以直接下载,然后上传到指定的目录。

解压nginx压缩包

tar -zxvf nginx-1.7.4.tar.gz 

会产生一个nginx-1.7.4 目录,这时进入nginx-1.7.4目录

cd  nginx-1.7.4

接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装

#默认安装在/usr/local/nginx
./configure
make
make install

如果没有报错,顺利完成后,最好看一下nginx的安装目录

whereis nginx 

(转)Centos7 Nginx安装

安装完毕后,进入安装后目录(/usr/local/nginx)便可以启动或停止它了。

到此,使用CentOS安装nginx已经完成了,其实看看还是蛮简单的。

2 Nginx启动脚本

centos7使用systemd代替之前的systemv的启动脚本,可以说更简单。不再需要编写一长段脚本。

复制以下内容到/usr/lib/systemd/system/nginx.service

vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

即可使用systemctl start|stop|reload|restart nginx 进行启动重启等操作。

systemctl enable nginx即可实现开机启动。

systemctl disable nginx取消开机启动