一、程序安装
1、安装
程序安装 直接百度就行了
2、启动
cd /usr/local/nginx/sbin
./nginx
3、常见报错
a) nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)
解决:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
b) [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size:64 (这个64也可能是32或其他数字,看你的配置)
解决:
这是因为server_name配置的太长了 要么少写几个 要么提高上限
提高上限:在nginx.conf内找到http{ 在里面添加(或修改)
server_names_hash_bucket_size 128;
不一定是128,可能64就够了 反正要写32的倍数
二、创建全局软链接
cd /
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
三、配置多站点
在nginx的conf目录下(或自己喜欢在什么地方)创建一个目录用来存放多个站点的配置文件,然后创建站点test.conf文件
cd /usr/local/nginx/conf
mkdir vhost
cd vhost
vim test.conf
然后编辑站点内容
示例:
保存退出 回到上级目录 编辑nginx.conf文件,在http {内添加一行代码 引入vhost内的所有conf文件
然后重启nginx就可以了
nginx -s reload
四、nginx开机自启动
1、进入 /lib/systemd/system/ 目录下,创建nginx.service文件
cd /lib/systemd/system
vim 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
ExecStart:该服务运行命令
ExecReload:重启命令
ExecStop:停止命令
就备注这三个 其他的要了解自行查询哈
编辑完后保存并退出
2、加入/取消开机自启动
# 开启
systemctl enable nginx.service
# 关闭
systemctl disable nginx.service
其它指令
systemctl start nginx.service 启动nginx
systemctl stop nginx.service 停止nginx
systemctl restart nginx.service 重启nginx
systemctl status nginx.service 查看当前状态