本次搭建主要是想试一试php的环境部署, nginx做服务接收端, php做请求处理, mysql作为数据库支持. 因此安装主要围绕这三个组件安装进行, 再把他们串联起来, 最后在浏览器通过访问php页面展示mysql数据库中的记录, 完成本次实践.
MySql和nginx之前玩过, php是第一次接触, 所以php环境搭建也是第一次, 记录以备忘.
搭建环境是CentOs 6.5, 在其他环境下应该也是大同小异, 只是命令会略有不同.
~# yum install mysql-server
~# mysql_install_db
~#/usr/bin/mysql_secure_installation
~#mysql -u root -p
mysql> create database lqp_db;
mysql> grant all privileges on lqp_db.* to ‘lqp’@’localhost’ identified by “111111”;
mysql> show schemas;
+——————–+
| Database |
+——————–+
| information_schema |
| lqp_db |
| test |
+——————–+
mysql> use lqp_db;
Database changed
mysql> select database();
+————+
| database() |
+————+
| lqp_db |
+————+
1 row in set (0.00 sec)
mysql> create table lqp_names ( _id int not null auto_increment, name varchar(40), school varchar(10), primary key (_id));
Query OK, 0 rows affected (0.09 sec)
mysql> insert into lqp_names(name, school) values(“lqp”, “JianxiCai”);
Query OK, 1 row affected (0.00 sec)mysql> insert into lqp_names(name,school) values (“zxd”,”dddddd”);
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye
准备好数据后, 下一步安装php模块.
~#yum install php-mysql
~# yum install php-fpm
~#vim /etc/php.ini
cgi.fix_pathinfo=1 -> cgi.fix_pathinfo=0
~#service php-fpm restart
~#yum install nginx
~# vim /etc/nginx/conf.d/default.conf
#
# The default server
#
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 {
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
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;
}
}
这里添加了最后一段, 在这里通过php-fpm的监听端口连接起来了:
location ~ \.php$ {
root /usr/share/nginx/html; #php文件根目录
fastcgi_pass 127.0.0.1:9000; #上面配置好的fpm模块监听端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
~#/etc/init.d/nginx start
启动nginx, 启动后, 可以在浏览器输入127.0.0.1回车就能看到nginx自带的主页了, 但是还没有php文件. 下面在(/usr/share/nginx/html)下编辑一个, 并且在php文件中访问mysql的数据.
~# pwd
/usr/share/nginx/html
~# vim index.php
echo "lqp is here
" ?>
$servername = "localhost";
$username = "lqp";
$password = "111111";
$dbname = "lqp_db";
$break = "
";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully".$break;
$sql = "SELECT _id, name, school FROM lqp_names";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["_id"]. " - Name: " . $row["name"]. " " . $row["school"].$break;
}
} else {
echo "0 results";
}
$conn->close();
?>
启动浏览器, 输入http://127.0.0.1/index.php
页面显示:
lqp is here
Connected successfully
id: 1 - Name: lqp JianxiCai
id: 2 - Name: zxd dddddd
通过以上步骤完成了php环境的搭建, 除此之外, nginx还可以用来将请求进一步转给其他服务模块比如tomcat或者jetty服务等, 这样可以在同一入nginx口下部署不同开发环境的服务, 还是非常方便的. 顺带说一句, php开发确实是很方便.