选择搭建环境
LNMP 环境
LNMP 是 Linux、Nginx、MySQL 和 PHP 的缩写,这个组合是最常见的 Web 服务器的运行环境之一。可以购买云服务器也可以自己先尝试本地搭建LNMP环境。
Linux:Linux 系统;
Nginx:Web 服务器程序,用来解析 Web 程序;
MySQL:一个数据库管理系统;
PHP:Web 服务器生成网页的程序。
用远程登录软件PuTTY 登录云服务器,在虚拟机上可忽略此步
使用 Yum 安装必要软件登录云服务器后,默认已获取 root 权限。
‘在 root 权限下,通过以下命令,先将必要软件一起安装 (Nginx、MySQL、PHP):
yum install nginx php php-fpm php-mysql mysql-server -y
安装完成,PuTTY 窗口会提示“Complete!”。同时可以上滑滚动条查看当前安装包版本:
本教程中安装包版本分别如下: Nginx:1.10.2 MySQL:5.1.73 PHP:5.3.3将各软件设置为开机启动:
chkconfig nginx on
chkconfig mysqld on
chkconfig php-fpm on
软件配置将 Nginx、MySQL、PHP 等各软件安装好之后,还需要对各软件分别进行配置。以下是详细步骤:
配置 Nginx请使用 Vim 命令打开default.conf文件,取消对 IPv6 地址的监听同时配置 Nginx,实现与 PHP 的联动。
vim /etc/nginx/conf.d/default.conf
按字母“I”键或 “Insert” 键切换至编辑模式,将已有内容全部清除,复制并粘贴以下内容到 default.conf文件。
server {listen 80;root /usr/share/nginx/html;server_name localhost;
#charset koi8-r;#access_log /var/log/nginx/log/host.access.log main;
location / {
index index.php 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 /usr/share/nginx/html;
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
} index index.php index.html index.htm;
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
修改完成后,按 “Esc” 键,输入 “:wq”,保存文件并返回
启动 Nginx。
service nginxstart
配置 MySQL
启动 MySQL 服务器。
service mysqldstart
设置 MySQL 服务器 root 用户的密码,本教程设置为 “123456”,后续步骤中需要用到此用户名和密码。
/usr/bin/mysqladmin -u root password"123456"
配置 PHP
启动 PHP-FPM 服务。
service php-fpmstart
配置 PHP Session 的存储路径。
打开/etc/php.ini文件。
vim/etc/php.ini
进入后直接输入以下内容,回车定位到 “session.save_path” 的位置:
/session.save_path
按字母“I”键或 “Insert” 键切换至编辑模式,将其改为 :
session.save_path ="/var/lib/php/session"
更改/var/lib/php/session目录下所有文件的属组都改成 nginx 和 nginx。
chown-R nginx:nginx /var/lib/php/session
验证环境配置
请使用以下命令在 Web 目录下创建index.php文件:
vim/usr/share/nginx/html/index.php
按字母“I”键或 “Insert” 键切换至编辑模式,写入如下内容:
<?phpecho"<title>Test Page</title>";echo"Hello World!";?>
输入完成后,按“Esc”键,输入 “:wq”,保存文件并返回。
在浏览器中,访问该index.php文件,查看环境配置是否成功:
http://云服务器实例的公网 IP/index.php
页面显示 “Hello World!”,则说明 LNMP 环境配置成功。
大部分为小帮搬运,但根据此教程是可以成功搭建个人网站的,最好有点Linux基础。