如何在服务器上设置LNMP环境?
我正在参加「掘金·启航计划」
介绍一下:
LNMP:是指一组通常一起使用来运行动态网站或者服务器的*软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP。
安装 Nginx
使用 yum
安装 Nginx:
yum install nginx -y
可能会报错,报错信息:
没有可用软件包 nginx。
错误:无须任何处理
解决办法
-
epel-release 是 CentOS 中经常用到的包
yum install epel-release -y
-
查看是否有 nginx 软件包
yum list | grep nginx
继续执行安装命令,并查看是否安装成功
nginx -v
将 Nginx 设置为开机自动启动:
systemctl enable nginx
启动
nginx
浏览器访问 IP 地址,可以看到下面页面。
至此 nginx 就安装成功了。
常用命令
查看是否启动成功
ps -ef | grep nginx
停止
nginx -s stop
相关文件夹
rpm -ql nginx
重新加载配置
nginx -s reload
重启 nginx 服务 修改配置完成后,重启 nginx 服务
service nginx restart
Nginx 设置开启、关闭、重启、开机自启
-
设置nginx开机自启
systemctl enable nginx
-
开启nginx
systemctl start nginx
-
查看nginx状态
systemctl status nginx
-
关闭nginx
systemctl stop nginx
-
重启nginx
systemctl restart nginx
注:不同环境的操作指令略有不同
搭建 Nginx 静态服务器
站点文件配置
创建 2 个站点文件夹
mkdir -p /www/wwwroot/a && mkdir -p /www/wwwroot/b
创建两个站点 index 文件
站点 A
vi /www/wwwroot/a/index.html
内容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>站点 A </title>
</head>
<body>
This is a A website...
</body>
</html>
站点 B
vi /www/wwwroot/b/index.html
内容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>站点 B </title>
</head>
<body>
This is a B website...
</body>
</html>
配置 nginx 文件
打开配置文件
vi /etc/nginx/nginx.conf
看看 http 中有没有下面这条配置
http {
include /etc/nginx/conf.d/*.conf;
}
在 conf.d 文件下,分别配置两个站点信息
打开配置文件,配置 A 站点信息
vi /etc/nginx/conf.d/a.conf
配置内容如下,多个域名用空格分开
server {
listen 80;
server_name a.xxx.com;
root /www/wwwroot/a;
location / {
index index.html index.htm;
}
}
打开配置文件,配置 B 站点信息
vi /etc/nginx/conf.d/b.conf
配置内容如下
server {
listen 80;
server_name b.xxx.com;
root /www/wwwroot/b;
location / {
index index.html index.htm;
}
}
访问上面配置的两个域名,可以看到如下信息
查看目录结构
看一下我们配置了那些文件
安装 tree
yum install tree -y
查看树形结构命令
tree /www && tree /etc/nginx/conf.d/
内容如下
/www
└── wwwroot
├── a
│ └── index.html
└── b
└── index.html
3 directories, 2 files
/etc/nginx/conf.d/
├── a.conf
└── b.conf
0 directories, 2 files
搭建 PHP 环境
安装 PHP
使用 yum
安装 PHP:
yum install php php-fpm php-mysql -y
安装之后,启动 PHP-FPM 进程:
service php-fpm start
启动之后,可以使用下面的命令查看 PHP-FPM 进程监听哪个端口
netstat -nlpt | grep php-fpm
把 PHP-FPM 也设置成开机自动启动:
chkconfig php-fpm on
配置 Nginx 并运行 PHP 程序
重启 nginx 服务
修改配置完成后,重启 nginx 服务
service nginx restart
配置 php.conf
新建一个名为 php.conf 的文件:
vi /etc/nginx/conf.d/php.conf
并配置 Nginx 端口 ,配置示例如下:
server {
listen 8000;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root /usr/share/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
配置 info.php
这时候,我们就可以新建一个名为 info.php 的文件:
vi /usr/share/php/info.php
添加代码来检查 php 是否安装成功了:
<?php phpinfo(); ?>
此时,访问 localhost:8000/info.php ,其中 localhost 换成服务器 ip 地址。可浏览到我们刚刚创建的 info.php 页面了
报错信息1
Redirecting to /bin/systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
报错信息2
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
解决办法
查看进程
ps -A | grep nginx
杀死进程
kill -9 pid1
报错信息3
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
解决办法,重启后执行下面命令
nginx -c /etc/nginx/nginx.conf
安装 MySQL
使用 yum
安装 MySQL:
yum install mysql-server -y
将 MySQL 设置为开机自动启动:
chkconfig mysqld on
设置 root 账户密码
重启 MySQL 服务:
service mysqld restart
设置 MySQL 账户 root 密码:
/usr/bin/mysqladmin -u root password '123456'
查看版本信息
mysqladmin -V
卸载 Nginx
首先输入命令 ps -ef | grep nginx 检查一下nginx服务是否在运行。
ps -ef | grep nginx
如果在运行就停止运行
nginx -s stop
使用yum清理
yum remove nginx
查找、删除Nginx相关文件
查看Nginx相关文件:
whereis nginx
find查找相关文件
find / -name nginx
依次删除find查找到的所有目录:
rm -rf /usr/lib64/nginx /etc/nginx /usr/share/nginx
最后看一下 Nginx 默认配置文件,里面加了注释
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
# 配置 worker 进程的数量
worker_processes auto;
# 错误日志
error_log /var/log/nginx/error.log;
# pid 进程编号
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
# 配置工作模式,每一个 worker 进程最多可以同时处理多少个请求,生产环境建议 10000~50000
events {
worker_connections 1024;
}
# http 请求
http {
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;
tcp_nopush on;
tcp_nodelay on;
# 连接保持时间
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# 可以配置多个 server 服务
server {
# 端口号
listen 80;
# IPv6
listen [::]:80;
# 主机域名
server_name _;
# location 的根目录
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
- Nginx 采用的是多线程(单进程)、多线路 IO 复用模型。使用 I/O 多线路复用技术 Nginx,就成了“并发事件驱动“的服务器。
- Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程。
- master 进程能监控 worker 进程的运行状态,当work进程异常,会自动启动新的worker。worker进程负责处理代理请求。
- Nginx 可以解决浏览器的同源策略。我们访问的静态资源和动态资源,浏览器都认为访问的是 nginx ,也就是认为所有的请求是同源的。(不会出现跨域问题)
- Nginx 的 default_server 指令可以定义默认的 server 去处理一些没有匹配到 server_name 的请求,如果没有显式定义,则会选取第一个定义的 server 作为 default_server。
- default_server 参数从0.8.21版开始可用。在之前的版本中,应该使用" default"参数代替。
- 请注意 default_server 是监听端口的属性,而不是主机名的属性
配置 Nginx https 服务
server {
listen 80;
server_name www.xxx.com;
root /www/wwwroot/a;
# 把 http 的域名请求转成 https
return 301 https://$host$request_uri;
location / {
index index.html index.htm;
}
}
# HTTPS server
#
server {
# SSL 使用 443 端口
listen 443 ssl;
# SSL 证书绑定的域名
server_name localhost;
# 证书 pem 文件
ssl_certificate cert.pem;
# 证书 key 文件
ssl_certificate_key cert.key;
#启用 SSL Session 缓存
ssl_session_cache shared:SSL:1m;
# 缓存 SSL 握手产生的参数和加密密钥的时长
ssl_session_timeout 5m;
# 使用的加密套件类型
ssl_ciphers HIGH:!aNULL:!MD5;
# 加密套件优先选择服务器的加密套件
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
推荐阅读
-
如何在 Mac 上设置阿里云服务器?
-
如何在阿里云上修改服务器的CPU、内存和带宽设置?
-
超级详细的步骤教你如何在Linux上安装LNMP环境并部署WordPress
-
如何在LNMP环境下添加、移除虚拟主机并设置伪静态?详细教程在这里!
-
如何在服务器上设置LNMP环境?
-
一步一步教你如何在CentOS 6.8上安装LNMP环境(包含Nginx、MySQL和PHP)
-
在Ubuntu 20.04上设置LNMP环境的简单步骤
-
一步一步在Linux上设置LNMP环境来建立个人博客网站
-
在 CentOS 8 上设置 LNMP 环境的简易指南
-
设置 LNMP(Linux、Nginx、MySQL 和 PHP)服务器环境