Ubuntu 14.04 + nginx + php + mysql + phpmyadmin环境搭建,详细操作
版本:Ubuntu 14.04 nginx-1.12.2 mysql-5.6 php-5.6
1、安装nginx-1.12.2
1)依赖安装
sudo apt-get install openssl libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
2)添加用户
sudo useradd www
3)下载http://nginx.org/download/nginx-1.12.2.tar.gz 并解压
进入nginx-1.12.2所在目录,比如/home/cky
cd /home/cky
解压
tar xzf nginx-1.12.2.tar.gz
4)进入相应目录并编译
cd nginx-1.12.2
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module
5)安装
sudo make install
6)复制nginx到bin目录下,方便使用
sudo cp /usr/local/nginx/sbin/nginx /usr/bin/
7)启动
sudo nginx
8)其他常用操作
关闭nginx
sudo ginx -s stop
重读nginx配置文件
sudo nginx -s reload
9)测试,浏览器输入http://127.0.0.1弹出Welcome to nginx!即表示安装成功
2、安装php-fpm
sudo add-apt-repository ppa:ondrej/php
sudo apt-get -y update
sudo apt-get install php5.6-fpm
其他扩展
sudo apt-get install php5.6-curl
sudo apt-get install php5.6-xml
3、修改nginx配置文件以支持 PHP
#打开配置文件
sudo vi /usr/local/nginx/conf/nginx.conf
按键盘a进入编辑模式
更改内容1,第43行左右
location / {
root html;
index index.html index.htm;
}
更改为:
location / { root /www/wwwroot; index index.html index.htm index.php; if (!-e $request_filename){
#地址作为将参数rewrite到index.php上。tp框架接收s参数为controller和action,不少框架都利用这种方式来实现伪pathinfo模式(pathinfo为php功能,nginx并不支持)
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
} }
#上面的正则能解决不带xx.php的,这条正则是为了rewrite url中带index.php/admin/admin/login这种,思路是一样的,将index.php后的字符串当成参>数
location ~ /.*\.php/ {
rewrite ^(.*?/?)(.*\.php)(.*)$ /$2?s=$3 last;
break;
}
更改内容2,第65行左右
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
更改为:
location ~ \.php$ {
root /www/wwwroot/;
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
4、修改php配置文件
1)修改php5-fpm属组与监听端口,在 PHP 5.4 之后,php5-fpm并不是监听 9000 端口
sudo vi /etc/php/5.6/fpm/pool.d/www.conf
php5-fpm属组:
;user = www-data
;group = www-data
改为
user = www
group = www
更改监听端口:
;listen = /var/run/php5.6-fpm.sock
改为
listen = 127.0.0.1:9000
2)配置php-fpm
sudo vi /etc/php/5.6/fpm/php.ini
768行,将
;cgi.fix_pathinfo=1
改为(还要去掉分号)
cgi.fix_pathinfo=0
保存退出
5、安装mysql数据库
1)安装 mysql-client-core-5.6
sudo apt-get install mysql-client-core-5.6
2)安装 mysql-client-5.6
sudo apt-get install mysql-client-5.6
3)安装 mysql-server-5.6
sudo apt-get install mysql-server-5.6
安装过程中要设置root用户的密码
4)mysql相关操作
查看mysql进程是否运行
ps -ef | grep mysql
查看msyql 监听端口
netstat -tap | grep mysql
msyql服务停止
sudo service mysql stop
Mysql 服务启动
sudo service mysql start
Mysql 服务重启
sudo service mysql restart
5)设置Mysql 服务远程访问
sudo vi /etc/mysql/my.cnf
47行改为
#bind-address = 127.0.0.1
重启msyql
sudo service mysql restart
修改访问权限
进入mysql,输入如下命令,输入密码,进入mysql命令行
mysql -u root -p
授权root用户访问权限,并刷新权限,此处的root可用其它MySQL用户替换,pwd部分需替换为该用户对应的密码
grant all privileges on *.* to root@"%" identified by "pwd" with grant option;
flush privileges;
exit;
重启mysql服务
sudo service mysql restart
6、数据库中存储中文时出现乱码
1)进入mysql命令行,查看mysql 服务端编码
status
发现是latin1的编码方式非utf8,需要修改为 utf8编码方式
2)退出mysql命令行并修改配置文件
sudo vi /etc/mysql/my.cnf
在[client]下追加
default-character-set = utf8
在[mysqld]下追加
character-set-server = utf8
修改后如下
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
character-set-server = utf8
skip-external-locking
在[mysql]下追加
default-character-set = utf8
保存退出,重启mysql服务器
sudo service mysql restart
再次查看mysql服务端的编码方式
进入mysql的命令行再次输入:
status
确认latin1等的编码已改为utf8
7、安装phpmyadmin
1)安装mysqli扩展
sudo apt-get install php5.6-mysql
将phpmyadmin安装下载,并解压到/usr/local/phpmyadmin/目录
2)在解压文件中复制config.sample.inc.php为config.inc.php,按服务器中mysql的情形进行配置;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['connect_type'] = 'socket';
$cfg['Servers'][$i]['socket'] = '/tmp/mysqld.sock ';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
3)创建存放log的文件,路径
sudo touch /var/log/phpmyadmin-nginx.log
4)在nginx的安装目录/usr/local/nginx/conf目录下,创建nginx集成phpmyadmin的配置文件phpmyadmin.conf:
server { listen 888; server_name localhost; access_log /var/log/phpmyadmin-nginx.log; set $php_upstream '127.0.0.1:9000'; location / { root /usr/local/phpmyadmin; index index.php; } location ~ \.php$ { root /usr/local/phpmyadmin; fastcgi_pass $php_upstream; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
5)在nginx的配置文件/usr/local/nginx/conf/nginx.conf中包含phpmyadmin的配置文件:
include /usr/local/nginx/conf/phpmyadmin.conf;
6)重启nginx即可
7)访问http://127.0.0.1:888