Install and switch apache to nginx and php5 In debian

Source: Internet
Author: User
1: Preface in the past, debian was used as the development server as apache and needs to switch to nginx for work. Sometimes both of them should be used. Make a simple note. Note that this article is only on debian7 (wheezy. Other operating systems are configured and installed on other operating systems, which cannot guarantee consistency. For more information about apache installation and configuration, see the previous article. 2: Install nginxph

1: Preface in the past, debian was used as the development server as apache and needs to switch to nginx for work. Sometimes both of them should be used. Make a simple note. Note that this article is only On debian 7 (wheezy. Other operating systems are configured and installed on other operating systems, which cannot guarantee consistency. For more information about apache installation and configuration, see the previous article. 2: Install nginx ph

1:Preface

In the past, debian was used as the development server as apache, and the work needs to be switched to nginx. Sometimes both of them should be used. Make a simple note. Note that this article is only On debian 7 (wheezy. Other operating systems are configured and installed on other operating systems, which cannot guarantee consistency. For more information about apache installation and configuration, see the previous article.


2:InstallNginx php5-cgi php5-fpm spawn-fcgi

# Apt-get install nginx-full nginx-common php5-cgi php5-fpm spawn-fcgi


3:Configure various php. ini

You can see it in # cd/etc/php5,

Yes

/Etc/php5/apache2/php. ini

/Etc/php5/cgi/php. ini

/Etc/php5/cli/php. ini

/Etc/php5/fpm/php. ini correspond to different php. ini configurations.

# Vim/etc/php5/cgi/php. ini remove the comments of cgi. fix-pathinfo = 1


4:Configure nginx. conf

In/etc/nginx. conf

Copy the following content, or you can compare and modify it.

user www-data;worker_processes 12;pid /var/run/nginx.pid;worker_rlimit_nofile 51200;events {worker_connections 30760;# multi_accept on;}http {### Basic Settings##sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 60;types_hash_max_size 2048;client_max_body_size 80m;# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;default_type application/octet-stream;### Logging Settings##access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;### Gzip Settings##gzip on;gzip_disable "msie6";gzip_vary on;gzip_min_length 1k;# gzip_proxied any;gzip_comp_level 2;gzip_buffers 4 16k;gzip_http_version 1.1;gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;### nginx-naxsi config### Uncomment it if you installed nginx-naxsi###include /etc/nginx/naxsi_core.rules;### nginx-passenger config### Uncomment it if you installed nginx-passenger###passenger_root /usr;#passenger_ruby /usr/bin/ruby;### Virtual Host Configs##include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;}#mail {## See sample authentication script at:## http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript# ## auth_http localhost/auth.php;## pop3_capabilities "TOP" "USER";## imap_capabilities "IMAP4rev1" "UIDPLUS";# #server {#listen     localhost:110;#protocol   pop3;#proxy      on;#}# #server {#listen     localhost:143;#protocol   imap;#proxy      on;#}#}




5:Configure default

In/etc/nginx/sites-available/default, note that my configuration file below has written two comments out of the php5-cgi and opened the php5-fpm. I use a php5-fpm here.

If you need to use spawn-fcgi, you need the section of the php5-cgi plus. /Home/www is my www directory. You can change it to your www directory. Root/home/www. I opened the automatic index, autoindex.

# You may add here your# server {#...# }# statements for each of your virtual hosts to this file### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.##log_format access '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for';server {#listen   80; ## listen for ipv4; this line is default and implied#listen   [::]:80 default_server ipv6only=on; ## listen for ipv6root /home/www;index index.html index.htm index.php;# Make site accessible from http://localhost/server_name localhost;charset utf-8;    #server_name _test1.com _test2.com _test3.com    #error_page 400 /errpage/400.html;    #error_page 403 /errpage/403.html;    #error_page 404 /errpage/404.html;location ~ .*\.(git|jpg|jpeg|png|bmp|swf)$ {expires 1d;}location ~ .*\.(js|css)?$ {expires 1h;}location / {autoindex on;autoindex_exact_size on;autoindex_localtime on;# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri/ /index.html;# Uncomment to enable naxsi on this location# include /etc/nginx/naxsi.rules}location /doc/ {alias /usr/share/doc/;autoindex on;allow 127.0.0.1;allow ::1;deny all;}# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests#location /RequestDenied {#proxy_pass http://127.0.0.1:8080;    #}#error_page 404 /404.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/www;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#fastcgi_split_path_info ^(.+\.php)(/.+)$;## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini### With php5-cgi alone:#fastcgi_pass 127.0.0.1:9000;## With php5-fpm:#fastcgi_pass unix:/var/run/php5-fpm.sock;#fastcgi_index index.php;#include fastcgi_params;#}location ~ \.php$ {        #######with php5-cgi####################        #fastcgi_pass 127.0.0.1:9000;        ########################################        #######with php5-fpm####################        fastcgi_pass unix:/var/run/php5-fpm.sock;########################################        fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /home/www$fastcgi_script_name;include fastcgi_params;}access_log /var/log/nginx/access.log access;# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#listen 8000;#listen somename:8080;#server_name somename alias another.alias;#root html;#index index.html index.htm;##location / {#try_files $uri $uri/ =404;#}#}# HTTPS server##server {#listen 443;#server_name localhost;##root html;#index index.html index.htm;##ssl on;#ssl_certificate cert.pem;#ssl_certificate_key cert.key;##ssl_session_timeout 5m;##ssl_protocols SSLv3 TLSv1;#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;#ssl_prefer_server_ciphers on;##location / {#try_files $uri $uri/ =404;#}#}


6:Start

There are several startup modes, either spawn-fcgi or php5-fpm or apache


If it is self-starting php-fpm,

You can use the sysv-rc-conf tool to check whether it is in start-up (# apt-get install sysv-rc-conf). The default value is


Turn Off spawn-fcgi or php5-fpm

Spawn-fcgi

# Netstat-anp | grep 9000

# Killall-HUP php5-cgi

Php5-fpm

#/Etc/init. d/php5-fpm stop


If you want spawn-fcgi to support/etc/init. d/

Start spawn-fcgi mode on startup

# Vim/etc/rc. local added

/Usr/bin/spawn-fcgi-a 127.0.0.1-p 9000-u www-data-g www-data-f/usr/bin/php5-cgi-P/var/run/fastcgi-php.pid -C 4

The parameters are based on the performance of your machine. Here is the test machine.

Create a system startup Stop command

# Cd/etc/init. d/

# Touch fastcgi

Save the following file to the created fastcgi and support/etc/init. d/fastcgi start | stop | reload and other operations.

#!/bin/bash# DESC: NGINX php5 FastCGI(spawn-fcgi) init shell scriptPROVIDES=php5-cgiLIGHTTPD_FCGI=/usr/bin/spawn-fcgiSERVER_IP=127.0.0.1SERVER_PORT=9000SERVER_USER=www-dataSERVER_GROUP=www-dataPHP_CGI=/usr/bin/php5-cgiKILLALL=/usr/bin/killall. /lib/lsb/init-functionscmd=$1pcgi_start(){echo "Starting $PROVIDES..."$LIGHTTPD_FCGI -a $SERVER_IP -p $SERVER_PORT -u $SERVER_USER -g $SERVER_GROUP -f $PHP_CGI -C 15 -P /var/run/fastcgi-php.pidecho "Done."}pcgi_stop(){echo "Killing $PROVIDES..."$KILLALL $PROVIDESecho "Done."}pcgi_restart(){pcgi_stoppcgi_start}pcgi_status(){status_of_proc -p /var/run/fastcgi-php.pid "/usr/bin/php5-cgi" php5-cgi && exit 0 || exit $?}pcgi_help(){echo "Usage: $0 {start|stop|restart|status}"}case ${cmd} in[Ss][Tt][Aa][Rr][Tt]) pcgi_start;;[Ss][Tt][Oo][Pp]) pcgi_stop;;[Rr][Ee][Ss][Tt][Aa][Rr][Tt]) pcgi_restart;;[Ss][Tt][Aa][Tt][Uu][Ss]) pcgi_status ;;*)      pcgi_help ;;esac


Switch to spawn-fcgi if you want to stop php5-fpm

#/Etc/init. d/php5-fpm stop

Edit/etc/nginx/sites-available/default to put php5-fpm

# Fastcgi_pass unix:/var/run/php5-fpm.sock;

Comment out, open the php5-fcgi

Fastcgi_pass 127.0.0.1: 9000;

Save


Restart nginx Configuration

#/Etc/init. d/nginx reload

View phpinfo




Similar to stopping the php5-fpm, it's easier to turn nginx off to enable apache

#/Etc/init. d/nginx stop

#/Etc/init. d/apache2 restart




7: to configure multiple different projects, you can

Include/etc/nginx/sites-enabled/*; here, it is changed to another sub-configuration file to facilitate switch as needed. Then/etc/init. d/nginx reload.


8: If you want apache and nginx to work at the same time, let nginx go through port 80, apache go through port 8080, and modify/etc/apache/ports. in the conf file, change 80 to 8080. Similarly, modify the port number of the VM in/etc/apache/site-available/to 8080.

Namevirtualhost*: 8080
Listen 8080.

Restart apache and access it through xxx. xxx: 8080.


9: If nginx and php5-fpm with access to php page reported 502, check the configuration in/etc/php5/fpm/pool. d/www. conf,

For example, because my www directory is authorized to tom, you need to modify it here. Save it and restart fpm and nginx.

Listen. owner = tom
Listen. group = tom

Listen =/var/run/php5-fpm.sock;

Listen. allowed_clients = 127.0.0.1


10: If you want to disable nginx and php version display, prevent others from sniffing the vulnerability of the corresponding version.

Enable server_tokens when nginx is in the http section.

And delete the corresponding/etc/nginx/fastcgi_params $ nginx_version.

        fastcgi_param SERVER_SOFTWARE nginx;

Disable all php versions and find the php. ini (cgi, fpm, cli, and apache have four different php. ini files corresponding to different versions)

Shut down the following on to off and restart the corresponding fpm/cgi/apache and nginx servers.

Expose_php = Off

Curl -- head http: // 127.0.0.1/info. php

The nginx and php versions are invisible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.