Nginx安装及配置【Linux(centos)】
- 安装编译工具及库文件
# 管理员方式 运行下面的指令。没有安装yum的自己想办法安装一下就行
sudo yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
- 安装 PCRE
# 进入/usr/local/src目录
cd /usr/local/src
# 手动下载PCRE的看这里:https://sourceforge.net/projects/pcre/files/
# 选择安装一个合适的版本,目前最新的是8.45
sudo wget http://downloads.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
# 解压,没有权限的话,就sudo
tar zxvf pcre-8.45.tar.gz
# 进入PCRE目录
cd pcre-8.45
# 编译
sudo ./configure
sudo make && sudo make install
# 查看PCRE版本
sudo pcre-config --version
# 控制台输出下面的信息,说明PCRE安装及配置成功
8.45
- 安装 Nginx
下载地址:https://nginx.org/en/download.html
# 下载地址:https://nginx.org/en/download.html
# 如果下载不成功,就手动下载压缩包,然后上传到指定目录
cd /usr/local/src/
wget https://nginx.org/download/nginx-1.20.1.tar.gz
# 解压
sudo tar zxvf nginx-1.20.1.tar.gz
# 编译&安装
cd nginx-1.20.1
sudo ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.45
sudo make
sudo make install
# 查看nginx版本
/usr/local/webserver/nginx/sbin/nginx -v
# 如果出现下面的信息,说明nginx编译安装成功
nginx version: nginx/1.20.1
- Nginx 配置
# 创建nginx运行使用的用户信息:www 比如名字就叫www
cd /usr/local/webserver/nginx/conf
sudo /usr/sbin/groupadd www
sudo /usr/sbin/useradd -g www www
# 查看一下nginx.conf
cat nginx.conf
# ...出现文件内容
原始 nginx.conf 文件内容:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
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;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index 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 html;
}
# 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 /scripts$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;
# }
#}
}
按照教程,这一步会让大家替换 nginx.conf 配置文件 ,将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容。
大家根据个人实际情况更改配置文件即可。
不知道cpu核心线程数的,看这里,执行下面这条指令:
grep ‘core id’ /proc/cpuinfo | sort -u | wc -l
user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
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';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
- 启动 Nginx
# 启动nginx
/usr/local/webserver/nginx/sbin/nginx
# 如果修改了nginx.conf配置文件,记得reload一下配置文件,没有权限的话就sudo。
# reload指令是nginx服务启动的前提下才能执行成功
sudo /usr/local/webserver/nginx/sbin/nginx -s reload
# 重启nginx服务
sudo /usr/local/webserver/nginx/sbin/nginx -s restart
# 查看启动结果
ps -ef | grep nginx
# 查看结果
root 1507 1 0 14:04 ? 00:00:00 nginx: master process /usr/local/webserver/nginx/sbin/nginx
www 1524 1507 0 14:04 ? 00:00:00 nginx: worker process
www 1525 1507 0 14:04 ? 00:00:00 nginx: worker process
appadmin 1931 15673 0 14:10 pts/0 00:00:00 grep --color=auto nginx
我配置的是https的server,通过访问地址也可以查看nginx服务的状态
- 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
其他需要知道的事情
CentOS 查看系统 CPU 个数、核心数、线程数
1、查看 CPU 物理个数
grep 'physical id' /proc/cpuinfo | sort -u | wc -l
2、查看 CPU 核心数量
grep 'core id' /proc/cpuinfo | sort -u | wc -l
3、查看 CPU 线程数
grep 'processor' /proc/cpuinfo | sort -u | wc -l
4、查看 CPU 型号
dmidecode -s processor-version
5、查看 CPU 的详细信息:
cat /proc/cpuinfo