搭建 lnmp 环境之 nginx 篇
记录一下自己搭建lnmp环境的过程,入了好多坑,浪费了好多时间,记录一下。
一. 准备工作
1.1 创建目录:
- mkdir lnmp
- mkdir -p lnmp/logs
- mkdir -p lnmp/server
- mkdir -p lnmp/tools
- mkdir -p lnmp/www
1.2 安装 make 、gcc 环境等
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc
- yum -y install gcc automake autoconf libtool make
- yum install gcc gcc-c++ glibc
1.3 安装 pcre 、zlib、openssl等库
-
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
-
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
-
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
- yum install -y pcre pcre-devel #注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
- yum install -y zlib zlib-devel
- yum install -y openssl openssl-devel
1.4 为nginx添加用户及组
ginx的工作进程需要以某一个用户的权限进行运行,为了服务器安全,一般指定一个普通用户权限的账号做为nginx的运行角色,根据个人习惯,这里使用www用户做为nginx工作进程的用户,后续安装的PHP也以www用户做为工作进程用户,依次执行以下命令以创建用户及用户组
- groupadd -r www
- useradd -r -g www www
二. 编译安装nginx
将nginx-1.17.1.tar.gz拷贝至linux服务器。
2.1 解压:
- tar -zxvf nginx-1.17.1.tar.gz
- cd nginx-1.17.1
2.2 配置参数:
./configure \
--prefix=/usr/local/lnmp/server/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/usr/local/lnmp/logs/nginx/error.log \
--http-log-path=/usr/local/lnmp/logs/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
2.3 编译安装
- make && make install
2.4 启动
#切换到安装位置,也就是之前配置configure的
cd /usr/local/nginx
#启动
#注:nginx.conf 是 nginx 的默认配置文件。也可以使用 nginx -c 指定配置文件启动
sbin/nginx
#启动失败
按照提示创建对应client目录
#启动成功查看nginx进程(master进程和worker进程)
ps -ef | grep nginx
错误:
1、nginx:[emerg] mkdir() "/var/temp/nginx/client" failed (2:No such file or directory)
- mkdir -p /var/temp/nginx/client
2、nginx: [error] open() "/usr/local/nginx/logs/nginx.pid"
- mkdir logs
- sbin/nginx -c conf/nginx.conf
3、nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)(新装的系统 80端口未开放)
- firewall-cmd --zone=public --add-port=80/tcp --permanent
- systemctl stop firewalld.service
- systemctl start firewalld.service
三. 配置
本文配置的nginx支持多域名,每个域名的配置文件单独放在/usr/local/lnmp/server/nginx/conf/vhosts路径下,使用vim命令打开nginx.conf文件,对其内容进行修改
user www www;
worker_processes 2;
error_log /usr/local/lnmp/logs/nginx/error.log crit;
pid /usr/local/lnmp/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /usr/local/lnmp/logs/nginx/access.log main;
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 65;
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;
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;
log_format '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include /usr/local/lnmp/server/nginx/conf/vhosts/*.conf;
include /usr/local/lnmp/server/nginx/conf/proxy/*.conf;
上述配置中,引用了/alidata/server/nginx-1.12.2/conf/vhosts/目录下所有后缀名是.conf的配置文件,现在进入该目录编写一个默认配置文件default.conf,其内容是
server {
listen 80;
server_name localhost;
index index.html;
root /lnmp/www;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
#伪静态规则
include /usr/local/lnmp/server/nginx/conf/rewrite/default.conf;
access_log /usr/local/lnmp/logs/nginx/access/default.log;
error_log /usr/local/lnmp/logs/nginx/error/default.log;
}
其中引入了一个/alidata/server/nginx-1.12.2/conf/rewrite/default.conf文件,这个文件是用来编写rewrite规则的配置文件,用来实现伪静态,其内容是
rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2 last;
rewrite ^(.*)/data/(.*)\.(htm|php)$ 404.html last;
rewrite ^(.*)/attachment/(.*)\.(htm|php)$ 404.html last;
rewrite ^(.*)/html/(.*)\.(htm|php)$ 404.html last;
四. 系统配置
1. 加入系统环境变量
使用vim命令打开/etc/profile文件,在文件最末尾加上如下代码
export NGINX_HOME=/usr/local/lnmp/server/nginx
export PATH=$PATH:$NGINX_HOME/sbin
- 保存修改后,使用source命令重新加载配置文件,命令如下
source /etc/profile
2. 加入系统服务
- 使用vim命令在/etc/init.d/目录下创建一个nginx文件,命令如下
vim /etc/init.d/nginx
文件内容如下
#!/bin/bash
# chkconfig: - 85 15
PATH=/usr/local/lnmp/server/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
将上述文件保存后,一定要记得给其赋予可执行的权限,具体命令如下
chmod +x /etc/init.d/nginx
3.开机自启动
chkconfig --add nginx
chkconfig nginx on
或者
systemctl enable nginx
本作品采用《CC 协议》,转载必须注明作者和本文链接
啥时候出个离线版本 :joy: