yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
要安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解压安装包:
[root@bogon src]# tar zxvf pcre-8.35.tar.gz
3、进入安装包目录
[root@bogon src]# cd pcre-8.35
4、编译安装
[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install
5、查看pcre版本
[root@bogon pcre-8.35]# pcre-config --version
开始安装Nginx解压文件
cd /usr/local/
tar -zxvf nginx-1.23.2.tar.gz
mv nginx-1.23.2 nginx
编译和安装
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
说明:
nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。
make && make install
启动和关闭服务
./usr/local/nginx/sbin/nginx
./usr/local/nginx/sbin/nginx -s top
centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。关于Systemd的详情介绍在这里。Systemd服务文件以 .service结尾,比如现在要建立nginx为开机启动,如果用yum install
命令安装的,yum命令会自动创建nginx.service文件,直接用命令:1:systemcel enable nginx.service设置开机启动即可。
在这里我是用源码编译安装的,所以要手动创建nginx.service服务文件。
开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即:
vi /etc/systemd/system/nginx.service
内容如下:
[Unit]
Description=nginx
After=network.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 quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
***参数解释:*Description:描述服务 After: 描述服务类别 [Service] 服务运行参数的设置 Type=forking 是后台运行的形式 ExecStart 为服务的具体运行命令 ExecReload 为重启命令 ExecStop 为停止命令 PrivateTmp=True 表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
systemctl daemon-reload
设置开机启动
systemctl enable nginx.service
其它命令:
systemctl start nginx.service (启动nginx服务)
systemctl stop nginx.service (停止nginx服务)
systemctl disable nginx.service (停止开机自启动)
systemctl status nginx.service (查看服务当前状态)
systemctl restart nginx.service (重新启动服务)
systemctl list-units --type=service (查看所有已启动的服务)
vi /usr/local/nginx/conf/nginx.conf
配置内容如下:
server {
listen 80; #设置访问端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #设置网站源码文件目录,默认是nginx安装目录下的html文件
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
配置多个网站
说明:如需配置多个网站,在nginx.conf中再添加一个server框架即可,例如:
#网站1:
server {
listen 80; #设置访问端口
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #设置网站源码文件目录,默认是nginx安装目录下的html文件
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#网站2:
server {
listen 81; #设置访问端口
server_name localhost;
location / {
root html; #设置网站源码文件目录,默认是nginx安装目录下的html文件
index index.html index.htm;
}
}