本文主要记录如何在
CentOS 7.6
中编译安装Nginx
官方最新的1.19.10
版本。由于像Nginx
、Mysql
和PHP
的的源码都是用C/C++
写的,所以自己的CentOS 7.6
服务器上必须要安装gcc
和g++
软件。搭建
LNMP
环境一般是先安装Mysql
/MariaDB
,再安装Nginx
,其次是安装PHP
准备工作
创建用户和组
先创建一个名为
nginx
且没有登录权限的系统用户和一个名为nginx
的系统用户组,然后安装nginx
所需的依赖库和依赖包,最后通过.configure
进行安装的详细配置。
- 创建
nginx
系统用户和系统用户组
groupadd -r nginx && useradd -c "Nginx Server" -r -g nginx -s /sbin/nologin -d /usr/local/nginx nginx -M
创建相关目录
# 创建缓存目录和日志目录
mkdir -pv /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp} /var/log/nginx
# 赋予nginx用户权限
chown -R nginx:nginx /var/cache/nginx /var/log/nginx
安装相关包
安装依赖库
Yum
安装GCC
、GCC-C++
、C/C++
语言编译环境
yum -y install gcc gcc-c++ make autoconf automake
Yum
安装Nginx
必须的依赖库
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel libxslt-devel libxml2 libxml2-devel gd-devel geoip-devel perl-devel perl-ExtUtils-Embed libuuid-devel
编译安装 Nginx
- 下载并解压文件
(备用:https://blog.xiaoqy.com/pub/packages/nginx/nginx-1.19.10.tar.gz)
wget -P '/usr/local/src' http://nginx.org/download/nginx-1.19.10.tar.gz \
&& cd /usr/local/src \
&& tar -zxvf nginx-1.19.10.tar.gz -C '/usr/local/src' \
&& cd nginx-1.19.10
- 预编译
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_ssl_module \
--with-http_v2_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_perl_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-pcre \
--with-pcre-jit \
--with-debug \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-threads \
--with-file-aio
- 编译并安装
make && make install
配置 Nginx
- 备份
nginx.conf
配置源文件
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old
- 修改文件
nginx.conf
,设置user
参数如下:user nginx;
sed -i 's/^#user\s\+nobody;$/user\ nginx;/g' /etc/nginx/nginx.conf
- 设置
Nginx
为系统服务
vim /usr/lib/systemd/system/nginx.service
添加以下内容:
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 设置
Nginx
为自启动并启动服务
systemctl enable nginx.service && systemctl start nginx.service
- 查询
Nginx
状态
systemctl status nginx.service
# 或者
ps -ef | grep nginx
netstat -anp | grep nginx
- 修改
nginx
配置文件
vim /usr/local/nginx/conf/nginx.conf
修改内容如下:
# 运行用户
user nginx;
# 启动进程, 通常设置成和cpu的数据相等
worker_processes 1;
# 全局错误日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 工作模式及连接数上限
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]";
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
server {
listen 80;
server_name www.xiaoqy.com;
#charset koi8-r;
access_log logs/nginx.dev.access.log main;
location / {
root /data/www/html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/www/html;
}
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
expires 30d;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
CentOS
添加开放端口
# 加入开放端口到配置文件
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 添加时区
--zone=public
# 添加端口
--add-port=80/tcp
# 永久生效
--permanent
# 加载防火墙新配置文件( 以root身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息. )
firewall-cmd --reload
作者:白小七羽
原文链接:https://blog.xiaoqy.com/143.html