1.在ubuntu下安裝配置nginx, mysql, php
安裝步驟:
參考:https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04#
具體摘抄如下:
About Lemp
LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running Ubuntu, the linux part is taken care of. Here is how to install the rest.
Setup
The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.
Step One—Update Apt-Get
Throughout this tutorial we will be using apt-get as an installer for all the server programs. On May 8th, 2012, a serious php vulnerability was discovered, and it is important that we download all of the latest patched software to protect the virtual private server.
Let's do a thorough update.
sudo apt-get updateStep Two—Install MySQL
MySQL is a powerful database management system used for organizing and retrieving data
To install MySQL, open terminal and type in these commands:
sudo apt-get install mysql-server php5-mysqlDuring the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.
Once you have installed MySQL, we should activate it with this command:
sudo mysql_install_dbFinish up by running the MySQL set up script:
sudo /usr/bin/mysql_secure_installationThe prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none): OK, successfully used password, moving on...Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.
It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.
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...Once you're done with that you can finish up by installing PHP.
Step Three—Install nginx
Once MySQL is all set up, we can move on to installing nginx on the VPS.
sudo apt-get install nginxnginx does not start on its own. To get nginx running, type:
sudo service nginx startYou can confirm that nginx has installed an your web server by directing your browser to your IP address.
You can run the following command to reveal your VPS's IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'Step Four—Install PHP
To install PHP-FPM, open terminal and type in these commands. We will configure the details of nginx and php details in the next step:
sudo apt-get install php5-fpmStep Five—Configure php
We need to make one small change in the php configuration.Open up php.ini:
sudo nano /etc/php5/fpm/php.iniFind the line, cgi.fix_pathinfo=1, and change the 1 to 0.
cgi.fix_pathinfo=0If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit. We need to make another small change in the php5-fpm configuration.Open up www.conf:
sudo nano /etc/php5/fpm/pool.d/www.confFind the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.
listen = /var/run/php5-fpm.sockSave and Exit.
Restart php-fpm:
sudo service php5-fpm restartStep Six—Configure nginx
Open up the default virtual host file.
sudo nano /etc/nginx/sites-available/defaultThe configuration should include the changes below (the details of the changes are under the config information):
UPDATE: Newer Ubuntu versions create a directory called 'html' instead of 'www' by default. If /usr/share/nginx/www does not exist, it's probably called html. Make sure you update your configuration appropriately.
[...] server { listen 80; root /usr/share/nginx/www; index index.php index.html index.htm; server_name example.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; } # pass the PHP scripts to FastCGI server listening on the php-fpm socket 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; } } [...]Here are the details of the changes:
Save and Exit
We can quickly see all of the details of the new php configuration.
To set this up, first create a new file:
sudo nano /usr/share/nginx/www/info.php
Add in the following line:
<?php phpinfo(); ?>
Then Save and Exit.
Restart nginx
sudo service nginx restart
You can see the nginx and php-fpm configuration details by visiting http://youripaddress/info.php
Your LEMP stack is now set up and configured on your virtual private server.
2. 按照上述文章介紹的安裝步驟安裝之後,通過浏覽器訪問php文件得到502頁面:The page you are looking for is temporarily unavailable.
檢查nginx的error.log文件: 發現錯誤:connect to php5-fpm.sock failed (Permission denied),
檢查php5-fpm.sock的權限得到other的權限為:---
解決方法:
參考:http://stackoverflow.com/questions/23443398/nginx-error-connect-to-php5-fpm-sock-failed-13-permission-denied
具體摘抄如下:
Note: if your webserver runs as as user other than www-data, you will need to update the
www.conf
file accordingly
1.先檢查PHP FastCGI進程數是否夠用:
netstat -anpo|grep “php-cgi”|wc -l
如果輸出為0的話,則表示FastCGI 進程數夠大,
2.此時則修改scgi_params文件,找到:
scgi_param SCGI 1;
把它改為:
scgi_param SCGI 5;
3.PHP程序如果的執行時間超過了Nginx的等待時間,就可適當地增加nginx.conf配置文件中FastCGI的timeout時間,例如:
http
{
……
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k
fastcgi_buffers 4 64k
……
}
4.重啟FastCGI
先殺掉進程:# pkill -9 php-cgi
然後重啟:# /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/local/bin/php-cgi
5.重啟Nginx
先殺掉進程:# killall -9 nginx
然後重啟:# /usr/local/sbin/nginx
其它可能情況:
1)訪問任意PHP文件,出現
The page you are looking for is temporarily unavailable.
Please try again later.
2)訪問html頁面,正常
原因:
nginx不能正常通過FastCGI結果訪問PHP
1)如果是以tcp socket形式,可能是進程用戶權限設置得不對
spawn-fcgi -a 127.0.0.1 -p 9000 -C 2 -u www-data -g www-data -f /usr/bin/php-cgi
可以改為 www-data 或者 nobody, 重啟php-cgi進程
2)如果是unix socket,可能 socket文件權限沒有寫入能力
srwxrwxr-x 1 gavin gavin 0 11-12 10:18 php-fcgi.sock
為其他用戶添加寫入能力
chmod o+w php-fcgi.sock
參考資料:hi.baidu.com/...7.html...余下全文>>
要想讓php-fpm顯示錯誤日志,首先需要配置php-fpm。
在php-fpm的配置文件中(一般位於php安裝目錄下的etc/php-fpm.conf)配置php錯誤日志的文件路徑。
; Error log file; If it's set to "syslog", log is sent to syslogd instead of being written; in a local file.; Note: the default prefix is /home/wangwei/php/var; Default Value: log/php-fpm.log;error_log = log/php-fpm.log如上是我的php-fpm.conf文件中配置錯誤日志的地方。把error_log = log/php-fpm.log之前的;去掉,然後修改為:
; Error log file; If it's set to "syslog", log is sent to syslogd instead of being written; in a local file.; Note: the default prefix is /home/wangwei/php/var; Default Value: log/php-fpm.logerror_log = /home/work/log/php-fpm.log.wf修改之後,保存配置,然後重啟php-fpm就可以啦。
注意如果用相對路徑的話,的路徑的前綴是基於php安裝目錄的var目錄的。