Centos安装配置nginx

centos安装配置nginx

1. 安装环境

当在linux中安装nginx的时候,执行了./configure以后,如果出现以下错误,则需要安装环境

编译安装nginx需要pcre包,未安装会有如下提示:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

因为nginx为c++编写, 而且官方提供的为源码, 我们需要安装gcc等编译源码进行安装

安装环境 ,需要安装pcre的devel包,pcre-devel。

yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

安装了上面的环境, 就可以了, 如果编译的时候还是无法编译成功,

出现该错误:.....checking for C compiler ... not found

再安装此依赖

yum -y install gcc gcc-c++ autoconf automake make

2. 下载nginx

下载地址: http://nginx.org/en/download.html

这里提供两种下载方式

2.1 本地下载上传服务器

如下图, 红色框中的为linux或mac版本

下载之后上传到服务器

2.2 服务器直接下载

直接下载需要使用wegt命令, 如果没有此命令, 安装即可yum install wegt

首先在nginx官网下载按钮上鼠标右键点击, 选择复制链接地址

然后使用 wegt 链接地址 即可直接下载到服务器的当期目录

3. 安装nginx

下载完毕的文件解压, 然后进入到nginx目录中

执行目录中的configure文件用于生成可执行文件

./configure     # 在解压后的nginx目录中运行

编译

make

安装

make install

没报错的话就是安装完成了

然后使用whereis nginx指令来查找nginx的安装目录, 我的结果为/usr/local/nginx/目录下

4. 运行和关闭nginx

在nginx安装目录中, 有一个sbin的目录, 进入看到有一个nginx的可执行文件

必须sbin此目录中执行以下命令

./nginx            # 开启nginx
./nginx -s reload  # 重启nginx
./nginx -s stop    # 关闭nginx

5. nginx配置文件位置

配置文件在nginx安装目录下的conf目录中, 里面的nginx.conf文件就是配置文件

配置可参考 nginx反向代理

6. 创建快捷指令

创建文件 vim /usr/lib/systemd/system/nginx.service

写入如下内容

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.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 stop

[Install]
WantedBy=multi-user.target

注意路径, 一定要是你自己安装的nginx路径

如果其他用户需要此命令, 使用chmod指令添加权限即可

使文件生效systemctl daemon-reload或者重启系统reboot

nginx快捷指令如下, 就不用每次都到nginx安装目录下执行了

systemctl start nginx   # 开启nginx
systemctl stop nginx    # 关闭nginx
systemctl restart nginx # 重启nginx

nginx开机自动启动

systemctl enable nginx

关闭开机自动启动

systemctl disable nginx.service

查看当前状态

systemctl status nginx.service
原文地址:https://www.cnblogs.com/liqbk/p/13496818.html