Fork me on GitHub

搭建 LNMP 环境

LNMP是建立web应用的平台,是Linux、NGINX,MySQL(有时也指MariaDB,数据库软件) 和PHP(有时也是指Perl或Python) 的简称。

搭建 Nginx 静态服务器

安装 Nginx

使用 yum 安装 Nginx

1
yum -y install nginx

修改 /etc/nginx/conf.d/default.conf ,去除对 IPv6 地址的监听,可以参考下面的示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80 default_server;
# listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /{
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

修改完成后,启动 Nginx

1
nginx

此时,可以访问 http://\ 来确认是否安装成功

将 nginx 设置为开机自动启动

1
chkconfig nginx

安装 MySQL 数据库服务

使用 yum 安装 MySQL :

1
yum -y install mysql-server

安装完成后,启动 MySQL 服务

1
service mysqld start

设置 MySQL 管理员密码

1
/usr/bin/mysqladmin -u root password 'QuZheng'

将 MySQL 设置开机自动启动

1
chkconfig mysql on

安装 PHP

使用 yum 安装 PHP:

1
yum -y install php php-fpm php-mysql

安装之后,启动 php-fpm 进程:

1
service php-fpm start

启动之后,可以使用下面的命令查看 PHP-FPM 进程监听那个端口

1
netstat -nplt | grep php-fpm

将 PHP-FPM 设置开机自动启动

1
chkconfig php-fpm on

配置 Nginx 并运行 PHP 程序

配置 Nginx

/etc/nginx/conf.d 目录中新建一个名为 php.conf 的文件,并配置 Nginx 端口 ,配置示例如下:

1
2
3
4
5
6
7
8
9
10
11
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;
}
}

修改配置完成后,重启 nginx 服务

1
service nginx restart

这时候,我们就可以在/usr/share/php 目录下新建一个 info.php 文件来检查 php 是否安装成功了,文件内容参考如下:

1
<?php phpinfo(); ?>
坚持原创技术分享,您的支持将鼓励我继续创作
显示 Gitment 评论