Ubuntu下搭建Nginx+PHP+MySQL环境
概述
换了Mac,但是以后也需要在Linux下做开发了,从搭建环境开始,彻底告别Windows。这篇文章就是介绍如何在Linux下搭建Nginx+PHP+MySQL环境。老鸟请飘过~~
第一步:更新apt-get
apt-get是Ubuntu下的包管理管工具,命令如下:
sudo apt- get update
第二步:安装MySQL
命令:
sudo apt-get install mysql- server php5-mysql
在安装过程中,MySQL会要求我们设置root密码,如果不想在这里设置,可以在安装完成之后设置。
基本上到这里,MysQL就已经安装成功了,可以运行如下命令,进行额外设置:
sudo /usr/bin/mysql_secure_installation
命令执行示例:
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost' . This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
第三步:安装Nginx
命令:
sudo apt-get install nginx
安装完成之后,启动Nginx:
sudo service nginx start
在浏览器输入 localhost
即可看到Nginx的欢迎页面。
第四步:安装PHP
命令:
sudo apt-get install php5-fpm
第五步:配置PHP
PHP的配置文件路径为: /etc/php5/fpm/php.ini
,根据需要配置。
第六步:配置Nginx
这时候,Nginx还不能解析PHP文件,我们需要配置Nginx加载PHP-FPM,使其能解析PHP。
Nginx的配置文件路径为: etc/nginx/sites-available/default
,不是 /etc/nginx/nginx.conf
这个文件,我就是在这一步弄错了。
配置文件示例:
[ ... ]
server {
listen 80
root /usr/share/nginx/http://www.zjjv.com/
location / {
try_files $uri $uri/ /index.html
}
error_page 404 / 404. html
error_page 500 502 503 504 /50x.html
location = /50x.html {
root /usr/share/nginx/www
}
location ~ .php$ {
try_files $uri = 404
fastcgi_pass unix:/var/run/php5-fpm.sock
fastcgi_index index.php
fastcgi_param SCRIPT_FILENAME $document_root
$fastcgi_script_name
include fastcgi_params
}
}
[ ... ]
第七步:创建测试文件
Nginx默认的网站根目录为 /usr/share/nginx/html
,在该目录下创建一个demo.php文件,写入 phpinfo(
,测试文件是否能被解析,如果解析成功,则证明环境已经搭建成功。
总结
感觉还有很多问题,好苦逼~~
参考
1. Setting Up PHP behind Nginx with FastCGI
2. How To Install Linux, nginx, MySQL, PHP (LEMP stack on Ubuntu 12.04