CentOS 下手动编译 nginx 以及相关配置
1 安装 nginx
1.1 编译安装所需要的功能,需要 roo t权限,如果以安装则不需要这一步
yum -y install gcc gcc- c++ wget vim
1.2 安装 nginx
## 下载nginx安装包,版本根据自己需求自己替换
wget http://nginx.org/download/nginx-1.10.3.tar.gz
## 解压缩
tar -xvf nginx-1.10.3.tar.gz
## 进入解压后的目录
cd nginx-1.10.3
## 安装nginx所需功能
yum install -y pcre-devel openssl openssl-devel
## 配置信息,--prefix为安装文件的目录
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 --with-pcre
## 编译和安装
make && make install
1.3 nginx开机自启动
## 将nginx加入到systemctl中
vi /usr/lib/systemd/system/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 stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2 添加nginx环境变量
## 更改/etc/profile文件
vim /etc/profile
## 配置信息,在文件末尾加入下面语句,多个环境变量用:隔开
export PATH=$PATH:/usr/local/nginx/sbin
3 nginx基础配置--引入vhost配置,虚拟目录,很重要!
## 在nginx配置路径中建立vhost文件夹,/usr/local/nginx/conf
mkdir vhost
## 配置nginx.conf,路径/usr/local/nginx/conf
vim nginx.conf
## 在http{末尾加入}
include vhost/*.conf
4 nginx常用命令
## 每次配置nginx后检查是否配置成功
nginx -t
## 重启nginx
nginx -s reload
## systemctl命令
## 开机启动
systemctl enable *.service
## 取消开机启动
systemctl disable *.service
## 启动
systemctl start *.service
## 停止
systemctl stop *.service
5 nginx常见问题
## 1 若 nginx 启动成功,但浏览器无法访问,关闭防火墙,或开启相应端口
## 关闭防火墙
service firewalld stop
开启防火墙的80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
#需要重启防火墙生效 很重要
service firewalld restart
# or
service firewalld stop
service firewalld start
本作品采用《CC 协议》,转载必须注明作者和本文链接