测试环境:

[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 即可。
 


最新文章

  1. SVN中trunk、branches、tag的使用
  2. Telnet
  3. PHP集成百度Ueditor 1.4.3
  4. 9月26日JavaScript表单验证、正则表达
  5. Android--菜单详解
  6. MVC WebApi跨域ajax接受post数据笔记
  7. BestCoder Round #85 hdu5776 sum
  8. Vim+Taglist+Ctags(源码阅读).
  9. Ubuntu12.04 Eclipse 提示框背景色修改
  10. 菜鸟进阶——grunt
  11. java:数据结构(二)栈的应用(括号匹配)
  12. 关于在windows上远行的虚拟机为ubuntu16.04中不能复制和粘贴的问题解决方案
  13. Visual Studio提示“无法启动IIS Express Web服务器”的解决方法 调试闪退
  14. 20165225《Java程序设计》第三周学习总结
  15. TransitionsTest
  16. Apache 运行PHP原理
  17. 微信小程序-textarea中的文本读取以及换行问题
  18. css做鼠标指向图片图片放大但边框不放大
  19. poi导出excel,以字符串格式输出数字
  20. SQL Server 保存不了修改后的表的解决方法

热门文章

  1. 读HikariCP源码学Java(二)—— 因地制宜的改装版ArrayList:FastList
  2. 使用Flutter设计一个好看的"我"页面
  3. 「是时候升级Java11了」 JDK11优势和JDK选择
  4. 《手把手教你》系列基础篇之(一)-java+ selenium自动化测试-环境搭建(上)(详细教程)
  5. C#关于数据库中存储的用户权限类似 "普通员工,管理员" 如何在代码中读取分析权限
  6. Golang学习(用代码来学习) - 第五篇
  7. java入门了解、安装jdk及软件的选择
  8. ACM、OI、OJ题目常用代码模板
  9. jenkins pipeline的声明式与脚本式
  10. 9.6、zabbix监控总结