Install Nginx environment configuration in ubuntu

Source: Internet
Author: User
Tags bz2 character set chmod install php php and php and mysql socket zend

Install nginx

Sudo apt-get install nginx

After Ubuntu is installed, the file structure is roughly as follows:

All configuration files are under/etc/nginx, and each virtual host is already under/etc/nginx/sites-available.
Program file in/usr/sbin/nginx
Logs are stored in/var/log/nginx.
The startup script nginx has been created under/etc/init. d/.
The default virtual host directory is/var/www/nginx-default (in some versions, the default virtual host directory is/var/www, see the configuration in/etc/nginx/sites-available)

[Edit] start nginx

Sudo/etc/init. d/nginx start

Then you can access http: // localhost/. Everything is normal! If it cannot be accessed, do not continue. Check the cause and then continue. If port 80 is occupied at startup: Starting nginx: [emerg]: bind () to 0.0.0.0: 80 failed (98: Address already in use), modify the file: /etc/nginx/sites-available/default: remove the # sign before listen. # indicates the comment in this file, change the port number 80 after listen to your own Port. You need to add a port number for access.

(If 403 error occurs after installation, it may be that the website path in the nginx configuration file is incorrect)

Nginx default page

[Edit] configure php and mysql

[Edit] install Php and mysql

Install php and MySQL:

Sudo apt-get install php5-cli php5-cgi mysql-server php5-mysql

[Edit] FastCgi installation

The/usr/bin/spawn-fcgi file is used to manage FastCgi. It originally belongs to the lighttpd package, but after 9.10, spawn-fcgi is separated into a separate package:

Sudo apt-get install spawn-fcgi

[Edit] Configure nginx

Modify the nginx configuration file:/etc/nginx/sites-available/default modify the host name:

Server_name localhost;

Modify the row of index:

Index. php index.html index.htm;

Remove the following comments to support php scripts:

Location ~ . Php $ {
Include/etc/nginx/fastcgi_params; # The first line must be placed. Otherwise, an error occurs.
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/var/www/nginx-default $ fastcgi_script_name;
}

Restart nginx:

/Etc/init. d/nginx stop
/Etc/init. d/nginx start

Start fastcgi php:

Spawn-fcgi-a 127.0.0.1-p 9000-C 10-u www-data-f/usr/bin/php-cgi

To enable php-cgi to boot automatically: After Ubuntu is started, scripts in the/etc/rc. local file will be executed. Therefore, you can directly add the startup script to/etc/rc. local. Spawn-fcgi-a 127.0.0.1-p 9000-C 10-u www-data-f/usr/bin/php-cgi is added to the statement before exit 0.

Create and test phpinfo:

Sudo vi/var/www/nginx-default/info. php

<? Php phpinfo ();?>

Open http: // localhost/info. php.

Nginx phpinfo page

[Edit] install nginx + uwsgi + Django

Uwsgi 1.2 + django1.4 + nginx1.1.19 + ubuntu12.04 is used for installation this time.

First, you need to install newer software. If you are ubuntu11.04 and 11.10, the software version is different because of different kernels.

1. first install nginx: sudo apt-get install nginx 2. then install several others before uwsgi installation: sudo apt-get install mysql-server libxml2 libxml2-dev python-dev libpcre3 libpcre3-dev python-MySQLdb 3. install uwsgi: the software package may contain a 502 error after being installed in the ppa because the software version is not new enough:

Wget http://projects.unbit.it/downloads/uwsgi-1.2.tar.gz
Tar zxvf uwsgi-1.2.tar.gz
Cd uwsgi-1.2
Make
Cp uwsgi/usr/bin

Note: if: (Python. h: no file or directory exists)

Solution: sudo apt-get install python-dev

4. before installing django, I use ubuntu to install it. due to static admin management problems. therefore, it is better to download and install the SDK. google django then go to download https: // download/use chrome download default/home/helios/download then cd download then tar xzvf Django-1.4.tar.gz cd Django-1.4 sudo python setup. py install this is the latest version. copy official website installation method. then sudo cp django/bin/django-admin.py/usr/bin/next sudo mv/usr/bin/django-admin.py/usr/bin/django-admin this is to be consistent with the source installation on the operation I wonder if there is any problem. no problem found. if you have any questions, you can add a tutorial. yes. installed.

Create a project --------

1. cd 2. django-admin startproject mysite

|-Mysite, manage. py |-_ init _. py, settings. py, urls. py

Now we are talking about the admin static file. I will copy the static folder under/home/helios/download/Django-1.4/django/contrib/admin in the decompressed folder of the django installation package just downloaded to mysite
In this way, the localhost/admin page can be displayed normally and no longer has a style.

Another way to solve the admin static problem ---

Sudo chmod-R 777 folder path. This grants permissions to all subfolders in the folder.

You can install sudo apt-get install python-django from the source and then mkdir/home/helios/mysite/static/admin cp-rf/usr/lib/python2.7/dist-packages/ django/contrib/admin/media/home/helios/mysite/static/admin sudo chmod-R 777/home/helios/mysite/static/admin.

[Edit] Configure nginx uwsgi django

Environment description:

My computer user name: helios;

# Sudo vim/etc/nginx/sites-available/default

Nginx code appears: (do not copy the serial number)
1.

Listen 80;

2.

Root/home/helios/mysite;

3.

Server_name youdomain.com # domain name or local localhost;

4.

Location /{
Uwsgi_pass 127.0.0.1: 9000;
Include uwsgi_params;
Access_log off;
 }

5.

Location/static /{
Alias/home/helios/mysite/static;
Autoindex on;
Allow 127.0.0.1;
Deny all;
 }

6.

Location/static/admin /{
Alias/home/helios/mysite/static/admin;
Autoindex on;
Allow 127.0.0.1;
Deny all;
 }

7.

Location ~ ^. +. (Gif | jpg | png | ico | jpeg) $ {
Expires 3d;
 }

8.

Location ~ ^. +. (Css | js) $ {
Expires 12 h;
 }

Then

# Vim/etc/nginx. conf

1.

User helios;

2.

Remove the # sign at the beginning of gzip, except for Gzip setting.

Configure nginx now

[Edit] uwsgi configuration

# Cd/home/helios/mysite
# Vim wsgi. py

Write

Import OS
Import sys from django. core. handlers. wsgi
Import WSGIHandler

Sys. path. append ("/home/helios/mysite ")
OS. environ ["DJANGO_SETTINGS_MODULE"] = "mysite. settings"
Application = WSGIHandler ()

After completion

# Vim wsgi. xml

Write

<Uwsgi>
<Socket> 127.0.0.1: 9000 </socket>
<Listen> 80 </listen>
<Master> true </master>
<Pidfile>/var/run/nginx. pid </pidfile>
<Processes> 8 </processes>
<Pythonpath>/home/helios/mysite </pythonpath>
<Pythonpath>/home/helios/</pythonpath>
<Module> wsgi </module>
<Profiler> true </profiler>
<Memory-report> true </memory-report>
<Enable-threads> true </enable-threads>
<Logdate> true </logdate>
# This is the size occupied by the program. It cannot be too small. It is too large to be known yet.
# During the test, my computer cannot be visually tested under 300.
# You can also delete this section, because during the test, if this section is too small
# Libgcc_s.so.1 must be installed for pthread_cancel to work
# It is only tested in ubuntu 12.04, and other systems are unclear.
# In order not to be prone to errors, you can delete them, but this prompt is provided to friends in need.
<Limit-as> 300 </limit-as>
# Create a folder and create an empty file django. log.
<Daemonize>/home/helios/mysite/log/django. log </daemonize>
</Uwsgi>

[Edit] configure django

We can see these two steps in nginx configuration just now:

 

Location/static /{

Root/home/helios/mysite;
Access_log off;

} Location/static/admin /{

Root/home/helios/mysite/static;
Access_log off;

} Now the terminal:

Cd/home/helios/mysite
Vim settings. py

The two parameters ADMIN_MEDIA_PREFIX STATIC_URL point to here. If there are still problems, my parameters are not the two parameters. For more information, see: MEDIA_ROOT = '/home/helios/mysite/static' MEDIA_URL = '/static/'admin_media_prefix ='/static/admin/'and then the database problem: my mysql database is the terminal.

Cd/home/helios/mysite
Mysql-h localhost-u root-p

Enter password *****> create database mysite default character set utf8; (the default utf8 is used to record Chinese characters.)> quit

Vim settings. py

DATABASES = {

'Default ':{
'Engine ': 'Django. db. backends. Mysql', # Add 'postgresql _ psycopg2', 'postgresql ', 'mysqlite3', or 'oracle '.
'Name': 'mysite', # Or path to database file if using sqlite3.
'User': 'root', # Not used with sqlite3.
'Password': '*******', # Not used with sqlite3.
'Host': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'Port': '000000', # Set to empty string for default. Not used with sqlite3.
   }

There are also some: Language: LANGUAGE_CODE = 'zh-cn' Time: TIME_ZONE = 'Asia/Shanghai'

Other configuration friends about django can go to the network to search for django tutorials or the official website to see my blog there is a similar lazy installation script interested can go to see more detailed tutorial http://sishengbang.tk if my these if you have any questions, mail me: helio_s@163.com or yuyanget@gmail.com or my blog www. younguy. tk qq can also be added to me. However, nginx + ubuntu + django: 845262968

[Edit] no input file specified error

Sudo vi/etc/nginx/sites-available/default

This field

Location ~ . Php $ {
Root html;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/var/www/nginx-default $ fastcgi_script_name;
Include fastcgi_params;

Note:

Fastcgi_param SCRIPT_FILENAME/var/www/nginx-default $ fastcgi_script_name;

Change/var/www/nginx-default to the root directory of your website. The root directory under the server field is consistent with the website root directory

[Edit] install Zend Optimizer

PHP version 5.2 is required. PHP5.3 of Ubuntu10.04 is not supported. See PHP5.2. (Zend 5.5 supports PHP5.3, download address: http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz)

Download Zend Optimizer. Directly paste the download address, refer to the version number to change (this is a 32-bit), or the home page can only be registered

Http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz

Tar zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz

ZendOptimizer-3.3.9-linux-glibc23-i386 cd/data/5_2_x_comp
Sudo mkdir/usr/local/zend
Sudo cp ZendOptimizer. so/usr/local/zend

Edit php. ini

Sudo gedit/etc/php5/cgi/php. ini

Note that punctuation marks must start with English.

[Zend Optimizer]
Zend_optimizer.optimization_level = 1
Zend_extension = "/usr/local/zend/ZendOptimizer. so"

Disable php-cgi

Sudo killall-HUP php-cgi

Restart php-cgi

Spawn-fcgi-a 127.0.0.1-p 9000-C 10-u www-data-f/usr/bin/php-cgi

No need to restart nginx

Or the above phpinfo file, you need to see the following information

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
With Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies

[Edit] install XCache

Sudo apt-get install php5-xcache

Root @ ubuntu:/home/qii # dpkg-l | grep xcach
Ii php5-xcache 1.2.2-5 Fast, stable PHP opcode cacher

The xcache configuration file path is

/Etc/php5/conf. d/xcache. ini

Edit php. ini

Sudo gedit/etc/php5/cgi/php. ini

Add the xcache. ini content to php. ini.

Disable php-cgi

Sudo killall-HUP php-cgi

Restart php-cgi

Spawn-fcgi-a 127.0.0.1-p 9000-C 10-u www-data-f/usr/bin/php-cgi

You do not need to restart nginx to check whether the installation is successful.

Root @ ubuntu:/home/qii # php-v
PHP 5.2.10-2ubuntu6 with Suhosin-Patch 0.9.7 (cli) (built: Oct 23 2009 16:30:10)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
With XCache v1.2.2, Copyright (c) 2005-2007, by mOo

The previous info. php page should contain the XCache module.

XCache module on info page

It is a bit strange here that apache can load xcache without adding the XCache. ini content to php. ini, but there is no XCache module in info. php.

[Edit] install eAccelerator

Sudo apt-get install php5-dev

Download eAccelerator

Wget http://sourceforge.net/projects/eaccelerator/files/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.tar.bz2

Tar jxvf eaccelerator-0.9.6.1.tar.bz2

Cd eaccelerator-0.9.6.1

Phpize
Sudo./configure-enable-eaccelerator = shared
Sudo make

Qii @ ubuntu :~ // Tmp/eaccelerator-0.9.6.1 $ sudo make install
Installing shared extensions:/usr/lib/php5/20090626 + lfs/

Modify the php. Ini file and install it as the Zend extension. It is best to put it at the beginning and put it before [zend] to avoid inexplicable problems:

Sudo vi/etc/php5/cgi/php. ini

[Eaccelerator]
Zend_extension = "/usr/lib/php5/20090626 + lfs/eaccelerator. so"
Eaccelerator. shm_size = "16"
Eaccelerator. cache_dir = "/tmp/eaccelerator"
Eaccelerator. enable = "1"
Eaccelerator. optimizer = "1"
Eaccelerator. check_mtime = "1"
Eaccelerator. debug = "0"
Eaccelerator. filter = ""
Eaccelerator. shm_max = "0"
Eaccelerator. shm_ttl = "0"
Eaccelerator. shm_prune_period = "0"
Eaccelerator. shm_only = "0"
Eaccelerator. compress = "1"
Eaccelerator. compress_level = "9"
Eaccelerator. allowed_admin_path = "/var/www/nginx-default/control. php"

Create cache Directory

Eaccelerator. cache_dir = "/var/cache/eaccelerator" the cache path is defined here.

The default value is/tmp/eaccelerator. This is very simple because anyone can write to this directory, but it is unwise because the directory is automatically cleared after restart. A better solution is/var/cache/eaccelerator. Create the directory and make sure it is writable to the user of the eAccelerator (generally, this user is your web server operator, and may be www-data ). Use the default value to continue:

Mkdir/tmp/eaccelerator

Chmod 777/tmp/eaccelerator

Change to/var/cache/eaccelerator.

Eaccelerator. cache_dir = "/var/cache/eaccelerator"

Sudo mkdir/var/cache/eaccelerator
Sudo chown root: www-data/var/cache/eaccelerator
Sudo chmod u = rwx, g = rwx, o =/var/cache/eaccelerator

Copy control file control. php to the website root directory

Sudo cp control. php/var/www/nginx-default/

Modify $ user and $ pw of control. php. The default values are admin and eAccelerator.

Sudo vi/var/www/nginx-default/control. php

Disable php-cgi

Sudo killall-HUP php-cgi

Restart php-cgi

Spawn-fcgi-a 127.0.0.1-p 9000-C 10-u www-data-f/usr/bin/php-cgi

You do not need to restart nginx to enable http: // localhost/control. php

EAccelerator control. php page

View the following fields on the info. php page:

This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
With eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

Shield Thunder

New

/Etc/nginx/agent. conf

If ($ http_user_agent ~ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. net clr 1.1.4322;. net clr 2.0.50727)") {return 404 ;}

Note that space and parentheses must be escaped using.

The site is being configured.

Include/etc/nginx/agent. conf;

 

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.