测试环境:

[root@centos-linux ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

[root@centos-linux ~]# php --version

PHP 7.2.32 (cli)

首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)

1、安装相关的依赖包。

yum install -y gcc-c++

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

2、下载Nginx

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

3、解压安装包

tar zxvf nginx-1.18.0.tar.gz

4、进入安装包目录

cd nginx-1.18.0/

5、编译安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

6、创建 Nginx 运行使用的用户 www:

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

7、nginx.conf最小配置(/usr/local/nginx/conf/nginx.conf),红色为新增内容

user www www;
worker_processes 1;
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;#站点目录
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

8、检查配置文件正确性的命令

# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

9、启动Nginx

/usr/local/nginx/sbin/nginx

10、站点访问

 11、Nginx常用命令

/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件

/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启Nginx

/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx

12、Nginx调用PHP

先启动php-fpm

systemctl start php-fpm  // 需要加入systemctl服务后才可以

  在配置文件中增加如下内容

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
     # include fastcgi.conf;  #或用此句替代以上两句。不通版本的nginx不一定都包含该文件,如果没有该配置文件,则使用上面两句话。
}

  

13、使用yum安装Nignx

// yum -y install epel-release  // 如果需要添加CentOS EPEL仓库
yum -y install nginx

启动Nginx

systemctl start nginx

14、卸载Nginx

查找nginx相关文件

find / -name nginx*

从源头删除

rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx

在使用yum清理

yum remove nginx

15、设置Nginx开机自启动

在/lib/systemd/system目录下,创建nginx.service文件

编辑nginx.service文件,增加如下内容

[Unit]
Description=nginx service
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
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
 
服务的启动/停止/刷新配置文件/查看状态 
# systemctl start nginx.service          // 启动nginx服务
# systemctl stop nginx.service  // 停止服务
# systemctl restart nginx.service  // 重新启动服务
# systemctl list-units --type=service // 查看所有已启动的服务
# systemctl status nginx.service // 查看服务当前状态
# systemctl enable nginx.service // 设置开机自启动
# systemctl disable nginx.service //停止开机自启动

可能遇到的错误提示:

Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
直接按照提示执行命令systemctl daemon-reload 即可。
 


centos7下安装、配置Nginx、设置Nginx开机自启动的更多相关文章

  1. Centos7下安装配置Redsocks

    Redsocks是一个开源的网络程序,代码依赖开源的libevent网络库.Redsocks允许你将所有TCP连接重定向到SOCKS或HTTPS代理,比如Shadowsocks(Centos7下安装配 ...

  2. Centos7系统下编写systemd脚本设置redis开机自启动

    今天想设置redis开机自启动,我觉得这样子比较好,但是在网上找了很长时间发现大家都是基于chkconfig的写法来设置的,并不能由systemd进程来统一管理,所以这里我自己编写了一个,希望大家可以 ...

  3. CentOS7下安装配置Nginx

    一:安装依赖项 1.pcre:2.openssl:3.zlib:4.gcc:可直接通过yum安装 二:创建nginx账户(可以配置nginx.conf的user为此账户) useradd nginx ...

  4. Centos下安装配置WordPress与nginx教程

    感谢大佬:https://www.howtoing.com/how-to-install-wordpress-with-nginx-on-centos-6-2 WordPress是一个免费和开源网站和 ...

  5. CentOS7下安装配置vncserver

    之前试了xmanager,不过好像和在centos6有很大不同,居然没成功,然后找到了vncserver,试了下,成了 参考:http://blog.csdn.net/jiangliqing1234/ ...

  6. Centos7 下安装配置tomcat7

    首先下载压缩包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.78/bin/apache-tomcat-7. ...

  7. Centos7下安装配置keepalived

    这里用的是两台设备做高可用 master服务器ip地址:192.168.12.78 slave服务器ip地址:192.168.12.79 虚拟ip(VIP,一个尚未占用的内网ip即可)地址:192.1 ...

  8. Linux(centos7)安装redis并设置redis开机自启动

    1.下载redis安装包 wget http://download.redis.io/releases/redis-4.0.6.tar.gz 2.解压安装包 tar -zxvf redis-4.0.6 ...

  9. centos7下安装配置prometheus

    prometheus官网:https://prometheus.io/download/ 搭建环境参考:https://blog.csdn.net/baidu_36943075/article/det ...

  10. Ubuntu14.04下安装redis-3.2.0以及开机自启动

    去官网下载Redis-3.2.0.tar.gz,将redis-3.2.0.tar.gz放入/opt目录下 解压redis-3.2.0.tar.gz xiaoyao@xiaoyao-virtual-ma ...

随机推荐

  1. Hive窗口函数保姆级教程

    在SQL中有一类函数叫做聚合函数,例如sum().avg().max()等等,这类函数可以将多行数据按照规则聚集为一行,一般来讲聚集后的行数是要少于聚集前的行数的.但是有时我们想要既显示聚集前的数据, ...

  2. 【VBA】延时函数

    延时函数(单位秒),例如延时0.5秒 1 Sub 延时() 2 delay (0.5) 3 Debug.Print "延时0.5秒" 4 End Sub 5 6 Sub delay ...

  3. noip模拟7[匹配·回家·寿司]

    这次考试状态好像还是没有回来,只拿了55pts,还全是第一题的功劳,就是一个小KMP,然后还让我给打错了 就很难受,while打成了if,然后wa掉45分考完立马拿回来了,悔死了,害 第二题爆零了,为 ...

  4. Devops 改变coding —— 安装个指定版本的 jenkins 发现和想象的不太一样?

    你好呀,我是小猿来也,一个刚开始折腾 Devops 的程序猿. 写在前面 前两天在池大那里看到了一段话,原话出自美团首席科学家夏华夏老师,具体内容我贴到了下面. 对于图片里的内容你们是怎么认为的呢?我 ...

  5. 20204107 孙嘉临 《PYTHON程序设计》实验四报告

    课程:<Python程序设计>班级: 2041姓名: 孙嘉临学号: 20204107实验教师:王志强实验日期:2020年6月29日必修/选修: 公选课 ##作为一个轻度游戏玩家,当然是要写 ...

  6. 20201123 《python程序设计》实验四报告

    20201123 2020-2021-2 <python程序设计>实验三报告 课程:<Python程序设计>班级:2011姓名:晏鹏捷学号:20201123实验教师:王志强实验 ...

  7. 15、mysql事物和引擎

    15.1.数据库事物介绍: 1.什么是数据库事物:

  8. NameServer 核心原理解析

    在之前的文章中,已经把 Broker.Producer 和 Conusmer 的部分源码和核心的机制介绍的差不多了,但是其实 RocketMQ 中还有一个比较关键但是我们平时很容易忽略的组件--Nam ...

  9. gitolite安装及配置

    1.客户端(1)使用ssh-keygen命令在当前用户根目录下生成密钥(2)将该密钥远程拷贝到gitserver服务器上:scp ~/.ssh/id_rsa.pub user@ip:/tmp/neu. ...

  10. hadoop学习(三)HDFS常用命令以及java操作HDFS

    一.HDFS的常用命令 1.查看根目录下的信息:./hadoop dfs -ls 2.查看根目录下的in目录中的内容:./hadoop dfs -ls in或者./hadoop dfs -ls ./i ...