Linux 6.5部署LNMP之Nginx服务:打造高性能Web服务器
access_log /var/log/nginx/access.log main;# 监听80端口server_name localhost;
- 本文目录导读:
- 1、 安装配置Nginx
- 2、 安装PHP和MySQL
- 3、 测试Nginx服务
- 4、 Nginx优化技巧
- 5、 总结
在互联网时代,网站的响应速度和稳定性已经成为用户选择产品和服务的重要标准。而Nginx作为一个高性能、轻量级的Web服务器,在这方面有着得天独厚的优势。本文将以Linux 6.5系统为例,介绍如何使用LNMP(即Linux+Nginx+MySQL+PHP)架构部署Nginx服务,并提供一些实用技巧。
1. 安装配置Nginx
首先需要安装并配置Nginx,具体步骤如下:
1)安装依赖包
“`
yum install gcc pcre-devel openssl-devel zlib-devel -y
2)下载并解压源码包
wget -1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
3)编译安装
./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module
make && make install
4)启动、停止、重启命令
/usr/local/nginx/sbin/nginx # 启动nginx服务
/usr/local/nginx/sbin/nginx -s stop # 停止nginx服务
/usr/local/nginx/sbin/nginx -s reload # 重启nginx服务(不中断现有连接)
/usr/local/nginx/sbin/nginx -s quit # 优雅停止nginx服务(等待现有连接结束后再停止)
5)配置文件修改
Nginx的主要配置文件为nginx.conf,可以根据需要进行修改。例如:
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80 default_server; # 监听80端口
server_name localhost; # 域名或IP地址
location / {
root html; # 网站根目录
index index.html index.htm; # 默认首页
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
2. 安装PHP和MySQL
在LNMP架构中,PHP是连接Web服务器和数据库的桥梁,而MySQL则是主要的关系型数据库。因此,安装和配置PHP和MySQL也是部署Nginx服务的重要步骤。
1)安装PHP
yum install php php-fpm -y
2)修改php.ini文件
vi /etc/php.ini
![Linux 6.5部署LNMP之Nginx服务:打造高性能Web服务器缩略图 Linux 6.5部署LNMP之Nginx服务:打造高性能Web服务器](https://www.72715.net/wp-content/uploads/2023/05/48602c273075de8b36c54a38baee7bc4.png)
找到以下行并取消注释:
cgi.fix_pathinfo=0
date.timezone = Asia/Shanghai
“`
3)启动php-fpm服务
systemctl start php-fpm.service
systemctl enable php-fpm.service # 开机自启动
4)安装MySQL
在CentOS 6.5上可以使用yum命令直接安装,例如:
yum install mysql-server mysql -y
5)修改my.cnf文件
在/etc/my.cnf中添加以下内容:
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
max_connections=1024 # 最大连接数(根据实际情况调整)
query_cache_type=1 # 查询缓存类型(开启后可以提高查询速度)
query_cache_size=64M # 查询缓存大小(根据实际情况调整)
innodb_buffer_pool_size=512M # InnoDB缓存池大小(根据实际情况调整)
log-bin=mysql-bin # 开启二进制日志记录
binlog-format=mixed # 日志格式为混合模式(可选项:ROW、STATEMENT、MIXED)
server-id = 1 # 设置服务器ID号,用于主从复制时区分各个服务器
6)启动MySQL服务
service mysqld start
chkconfig mysqld on # 开机自启动
3. 测试Nginx服务
完成以上步骤后,就可以测试Nginx服务是否正常工作了。可以使用curl命令或浏览器访问网站来测试。
1)使用curl命令
curl
如果返回以下内容,则说明Nginx已经成功响应请求:
…
2)使用浏览器
在浏览器中输入服务器IP地址或域名,如果能够正常显示网站内容,则说明Nginx已经部署成功了。
4. Nginx优化技巧
除了基本的安装和配置外,还有一些优化技巧可以提高Nginx的性能和稳定性。
1)开启gzip压缩
在nginx.conf文件中添加以下代码:
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 text/javascript application/x-httpd-php image/jpeg image/gif image/png;
2)限制客户端连接数
limit_conn_zone $binary_remote_addr zone=addr:10m; # 定义连接数限制区域(10m为内存大小)
…
limit_conn addr 10; # 每个IP地址最多允许10个连接
}
3)设置缓存
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=mycache:10m inactive=60m;
proxy_pass
proxy_set_header Host $host;
proxy_cache mycache;
proxy_cache_valid 200 60m; # 缓存200状态码的响应60分钟
expires 1h; # 设置浏览器缓存时间为1小时
5. 总结
本文介绍了如何在Linux 6.5系统上使用LNMP架构部署Nginx服务,并提供了一些实用技巧。通过这些步骤,可以快速搭建一个高性能、稳定的Web服务器,提高网站的响应速度和用户体验。