测试环境:
[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
# 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 //停止开机自启动
可能遇到的错误提示: