LEMP环境包含Linux, nginx, MySQL, PHP,用来搭建web服务器。本文总结如何在Centos 6下安装和配置nginx, MySQL以及PHP。
安装MySQL
yum install mysql-server -y
安装完成后,需要重新启动MySQL
/etc/init.d/mysqld restart
对MySQL做一些安全配置。第一次无需root密码,直接回车,然后设置并确认root密码,建议所有选择都选Y。
/usr/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
安装nginx
首先启用epel源
yum install epel-release -y
安装
yum install nginx -y
第一次启动nginx
/etc/init.d/nginx start
安装PHP
yum install php-fpm php-mysql -y
配置PHP
vi /etc/php.ini
找到cgi.fix_pathinfo=1
这一列,修改为
cgi.fix_pathinfo=0
配置nginx
配置nginx的虚拟主机,这里以默认虚拟主机的配置为例
vi /etc/nginx/conf.d/default.conf
将配置文件修改成如下的样子
#
# The default server
#
server {
listen 80;
server_name example.com;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/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$ {
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
vi /etc/php-fpm.d/www.conf
把user和group的值由apache替换为nginx
[...]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
[...]
重启php-fpm
service php-fpm restart
测试——创建一个phpinfo页面
vi /usr/share/nginx/html/info.php
加入下面的内容
<?php
phpinfo();
?>
重新启动nginx
service nginx restart
在浏览器地址栏输入http://127.0.0.1/info.php
注意用实际的主机IP地址替换127.0.0.1,如果出现下面的页面,表示已经大功告成了!
设置开机启动
chkconfig --levels 235 mysqld on
chkconfig --levels 235 nginx on
chkconfig --levels 235 php-fpm on
网友评论