Nginx是一个免费,开源,高性能的HTTP服务器。Nginx是其稳定性,功能集丰富,简单配置,资源消耗低。本教程演示如何在Fedora 16上安装PHP5+Nginx(PHP-FPM)+MySQL的服务器。
但是我不发出任何保证,它一定会正常工作!
1、初步说明
在本教程中使用的主机名与IP地址192.168.0.100 server1.example.com。这些设置可能会有所不同,所以你在适当情况下不得不更换他们。
2、安装的MySQL 5
首先,我们这样安装MySQL 5,终端输入命令:
yum install mysql mysql-server
然后创建MySQL系统启动链接(这样MySQL在系统启动时自动启动)启动MySQL服务器:
systemctl enable mysqld.service
systemctl start mysqld.service
现在,检查网络启用。运行
netstat -tap | grep mysql
如正常,会现实如下内容:
[root@server1 ~]# netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 1116/mysqld
[root@server1 ~]#
如果不是这样,编辑/etc/my.cnf文件并注释掉选项skip-networking:
vi /etc/my.cnf
[…]
#skip-networking
[…]
并重新启动MySQL服务器:
systemctl restart mysqld.service
运行
mysql_secure_installation
为根用户设置密码(否则任何人都可以访问你的MySQL数据库!):
[root@server1 ~]# 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): <– ENTER
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] <– ENTER
New password: <– yourrootsqlpassword
Re-enter new password: <– yourrootsqlpassword
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] <– ENTER
… 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] <– ENTER
… 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] <– ENTER
– 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] <– ENTER
… 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!
[root@server1 ~]#
3、安装Nginx的
直接运行如下命令:
yum install nginx
然后我们创建系统启动nginx的链接,并启动它:
systemctl enable nginx.service
systemctl start nginx.service
在您的Web服务器的IP地址或主机到浏览器(如http://192.168.0.100),你应该看到nginx的欢迎页面类型:
nginx-fedora
4、安装PHP5
需要安装一些列的模块,输入以下命令:
yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
然后打开的/etc/php.ini并设定cgi.fix_pathinfo = 0:
vi /etc/php.ini
[…]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP’s
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
[…]
(请阅读http://wiki.nginx.org/Pitfalls找出为什么应该这样做。)
此外,以避免类似以下错误
[13-Nov-2011 22:13:16] PHP Warning: phpinfo(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Europe/Berlin’ for ‘CET/1.0/no DST’ instead in /usr/share/nginx/html/info.php on line 2
… /var/log/php-fpm/www-error.log 中,当你调用一个PHP脚本在您的浏览器,你应该在/etc/php.ini文件中设置date.timezone:
[…]
[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = “Europe/Berlin”
[…]
你可以找到正确的时区为您的系统,运行命令:
cat /etc/sysconfig/clock
[root@server1 ~]# cat /etc/sysconfig/clock
ZONE=”Europe/Berlin”
[root@server1 ~]#
下一步创建系统启动或者PHP-FPM并启动它:
systemctl enable php-fpm.service
systemctl start php-fpm.service
PHP-FPM是一个守护进程运行FastCGI服务器的端口9000。
5、nginx设置
输入命令:
vi /etc/nginx/nginx.conf
配置是很容易理解。
你可以再以下网址了解更多配置信息:
http://wiki.codemongers.com/NginxFullExample
http://wiki.codemongers.com/NginxFullExample2
按照以下配置设置值:
[…]
worker_processes 4;
[…]
keepalive_timeout 2;
[…]
虚拟主机是指在服务器{}容器。默认的虚拟主机是指在该文件 /etc/nginx/conf.d/default.conf -让我们作如下修改:
[…]
server {
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}
[…]
保存和重启服务:
systemctl reload nginx.service
现在你可以建立一个探针文件进行测试
6、建立 PHP-FPM 使用一个 Unix Socket
vi /etc/php-fpm.d/www.conf
[…]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[…]
重启PHP-FPM
systemctl restart php-fpm.service
配置文件:
vi /etc/nginx/conf.d/default.conf
配置内容如下:
[…]
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[…]
重启Nginx:
systemctl reload nginx.service
文章参考:
(责任编辑:IT) |