使用 yum
安装 Nginx:php
yum install nginx -y
修改 /etc/nginx/conf.d/default.conf,去除对 IPv6 地址的监听,可参考下面的代码示例:html
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:mysql
nginx
此时,可访问实验机器外网 HTTP 服务(http://<您的 IP 地址>)来确认是否已经安装成功。nginx
将 Nginx 设置为开机自动启动:sql
chkconfig nginx on
CentOS 6 不支持 IPv6,须要取消对 IPv6 地址的监听,不然 Nginx 不能成功启动。数据库
使用 yum
安装 MySQL:服务器
yum install mysql-server -y
安装完成后,启动 MySQL 服务:php-fpm
service mysqld restart
设置 MySQL 帐户 root 密码:
rest
/usr/bin/mysqladmin -u root password 'Password'
将 MySQL 设置为开机自动启动:code
chkconfig mysqld on
使用 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
CentOS 6 默认已经安装了 PHP-FPM 及 PHP-MYSQL,上面命令执行的可能会提示已经安装。
PHP-FPM 默认监听 9000 端口
在 /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; } }
修改配置完成后,重启 nginx 服务
service nginx restart
这时候,咱们就能够在/usr/share/php 目录下新建一个 info.php 文件来检查 php 是否安装成功了,文件内容参考以下:
<?php phpinfo(); ?>
此时,访问 http://<您的 IP 地址>:8000/info.php 可浏览到咱们刚刚建立的 info.php 页面了
恭喜!您已经成功完成了搭建 LNMP 服务器。