Linux Nginx最基础的十大安装步骤

开发 前端
假期开始Linux Nginx的学习,首先碰到的就是环境的配置。所以有一个星期的时间基本是在用实践着Linux Nginx,下面的经验希望大家有所收获。

Linux Nginx安装步骤有很多人不是很了解,其实相关的命令掌握牢固了就可以顺利的进行相关安装。下面我们就来看看LinuxNginx的十个安装步骤,希望大家有所收获。

一、 编译安装PHP5.2.9所需的支持库

 

  1. tar zxvf libiconv-1.13.tar.gz  
  2. cd libiconv-1.13/  
  3. ./configure --prefix=/usr/local  
  4. make  
  5. make install  
  6. cd ../  
  7. tar zxvf libmcrypt-2.5.8.tar.gz  
  8. cd libmcrypt-2.5.8/  
  9. ./configure  
  10. make  
  11. make install  
  12. /sbin/ldconfig  
  13. cd libltdl/  
  14. ./configure --enable-ltdl-install  
  15. make  
  16. make install  
  17. cd ../../  
  18. tar zxvf mhash-0.9.9.9.tar.gz  
  19. cd mhash-0.9.9.9/  
  20. ./configure  
  21. make  
  22. make install  
  23. cd ../  
  24. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la  
  25. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so  
  26. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4  
  27. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8  
  28. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a  
  29. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la  
  30. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so  
  31. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2  
  32. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1  
  33. tar zxvf mcrypt-2.6.8.tar.gz  
  34. cd mcrypt-2.6.8/  
  35. /sbin/ldconfig  
  36. ./configure  
  37. make  
  38. make install  
  39. cd ../ 

 

二、编译安装MySQL 5.1.34扩展库

 

  1. /usr/sbin/groupadd mysql  
  2. /usr/sbin/useradd -g mysql mysql  
  3. tar zxvf mysql-5.1.34.tar.gz  
  4. cd mysql-5.1.34/  
  5. ./configure --prefix=/usr/local/webserver/mysql/ --enable-
    assembler 
    --with-extra-charsets=complex --enable-thread-safe-
    client --with-big-tables --with-readline --with-ssl --
    with-embedded-server --enable-local-infile 
    --with-plugins=innobase 
  6. make && make install  
  7. chmod +w /usr/local/webserver/mysql  
  8. chown -R mysql:mysql /usr/local/webserver/mysql  
  9. cd ../ 

 

MySql的安装此处就省略


三、编译安装PHP(FastCGI模式)

tar zxvf php-5.2.9.tar.gz
gzip -cd php-5.2.9-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1
cd php-5.2.9/
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../#p#

四、修改php-fpm配置文件

rm -f /usr/local/webserver/php/etc/php-fpm.conf
vi /usr/local/webserver/php/etc/php-fpm.conf

输入以下内容:

 

  1. <?xml version="1.0" ?>   
  2. <configuration>   
  3. <section name="global_options">   
  4. <value name="pid_file">/usr/local/webserver/php/logs/php-fpm.pid</value>   
  5. <value name="error_log">/usr/local/webserver/php/logs/php-fpm.log</value>   
  6. <value name="log_level">notice</value>   
  7. <value name="emergency_restart_threshold">10</value>   
  8. <value name="emergency_restart_interval">1m</value>   
  9. <value name="process_control_timeout">5s</value>   
  10. <value name="daemonize">yes</value>   
  11. </section>   
  12. <workers>   
  13. <section name="pool">   
  14. <value name="name">default</value>   
  15. <value name="listen_address">127.0.0.1:9000</value>   
  16. <value name="listen_options">   
  17. <value name="backlog">-1</value>   
  18. <value name="owner"></value>   
  19. <value name="group"></value>   
  20. <value name="mode">0666</value>   
  21. </value>   
  22. <value name="php_defines">   
  23. <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>   
  24. <value name="display_errors">1</value>   
  25. </value>   
  26. <value name="user">www</value>   
  27. <value name="group">www</value>   
  28. <value name="pm">   
  29. <value name="style">static</value>   
  30. <value name="max_children">128</value>   
  31. <value name="apache_like">   
  32. <value name="StartServers">20</value>   
  33. <value name="MinSpareServers">5</value>   
  34. <value name="MaxSpareServers">35</value>   
  35. </value>   
  36. </value>   
  37. <value name="request_terminate_timeout">0s</value>   
  38. <value name="request_slowlog_timeout">0s</value>   
  39. <value name="slowlog">logs/slow.log</value>   
  40. <value name="rlimit_files">51200</value>   
  41. <value name="rlimit_core">0</value>   
  42. <value name="chroot"></value>   
  43. <value name="chdir"></value>   
  44. <value name="catch_workers_output">yes</value>   
  45. <value name="max_requests">500</value>   
  46. <value name="allowed_clients">127.0.0.1</value>   
  47. <value name="environment">   
  48. <value name="HOSTNAME">$HOSTNAME</value>   
  49. <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>   
  50. <value name="TMP">/tmp</value>   
  51. <value name="TMPDIR">/tmp</value>   
  52. <value name="TEMP">/tmp</value>   
  53. <value name="OSTYPE">$OSTYPE</value>   
  54. <value name="MACHTYPE">$MACHTYPE</value>   
  55. <value name="MALLOC_CHECK_">2</value>   
  56. </value>   
  57. </section>   
  58. </workers>   
  59. </configuration> 

 

五、创建www用户组及www用户

 

  1. /usr/sbin/groupadd www  
  2. /usr/sbin/useradd -g www www 


六、编译安装Linux Nginx

tar zxvf nginx-0.6.35.tar.gz
cd nginx-0.6.35/
默认安装:
./configure
make && make install
默认情况下,Linux Nginx会被安装在 /usr/local/nginx。通过设定编译选项,你可以改变这个设定。

指定编译选项安装,如:
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

七、创建fcgi.conf文件

 

  1. rm -f /usr/local/webserver/nginx/conf/fcgi.conf  
  2. vi /usr/local/webserver/nginx/conf/fcgi.conf 

输入以下内容:

 

  1. fastcgi_param GATEWAY_INTERFACE CGI/1.1;  
  2. fastcgi_param SERVER_SOFTWARE nginx;  
  3. fastcgi_param QUERY_STRING $query_string;  
  4. fastcgi_param REQUEST_METHOD $request_method;  
  5. fastcgi_param CONTENT_TYPE $content_type;  
  6. fastcgi_param CONTENT_LENGTH $content_length;  
  7. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
  8. fastcgi_param SCRIPT_NAME $fastcgi_script_name;  
  9. fastcgi_param REQUEST_URI $request_uri;  
  10. fastcgi_param DOCUMENT_URI $document_uri;  
  11. fastcgi_param DOCUMENT_ROOT $document_root;  
  12. fastcgi_param SERVER_PROTOCOL $server_protocol;  
  13. fastcgi_param REMOTE_ADDR $remote_addr;  
  14. fastcgi_param REMOTE_PORT $remote_port;  
  15. fastcgi_param SERVER_ADDR $server_addr;  
  16. fastcgi_param SERVER_PORT $server_port;  
  17. fastcgi_param SERVER_NAME $server_name;  
  18. # PHP only, required if PHP was built with --enable-force-cgi-redirect  
  19. fastcgi_param REDIRECT_STATUS 200; 

 

八、编辑Nginx配置文件

 

  1. rm -f /usr/local/webserver/nginx/conf/nginx.conf  
  2. vi /usr/local/webserver/nginx/conf/nginx.conf 

Linux Nginx输入以下内容:

 

  1. user www www;  
  2. worker_processes 8;  
  3. error_log /usr/local/webserver/nginx/logs/logs/nginx_
    error.log crit;  
  4. pid /usr/local/webserver/nginx/logs/nginx.pid;  
  5. worker_rlimit_nofile 51200;  
  6. events {  
  7. use epoll;  
  8. worker_connections 51200;  
  9. }  
  10. http {  
  11. include mime.types;  
  12. default_type application/octet-stream;  
  13. charset utf-8;  
  14. server_names_hash_bucket_size 128;  
  15. client_header_buffer_size 32k;  
  16. large_client_header_buffers 4 32k;  
  17. client_max_body_size 8m;  
  18. sendfile on;  
  19. tcp_nopush on;  
  20. keepalive_timeout 60;  
  21. tcp_nodelay on;  
  22. fastcgi_connect_timeout 300;  
  23. fastcgi_send_timeout 300;  
  24. fastcgi_read_timeout 300;  
  25. fastcgi_buffer_size 64k;  
  26. fastcgi_buffers 8 64k;  
  27. fastcgi_busy_buffers_size 128k;  
  28. fastcgi_temp_file_write_size 128k;  
  29. gzip on;  
  30. gzip_min_length 1k;  
  31. gzip_buffers 4 16k;  
  32. gzip_http_version 1.0;  
  33. gzip_comp_level 2;  
  34. gzip_types text/plain application/x-javascript text/css 
    application/xml;  
  35. gzip_vary on;  
  36. #limit_zone crawler $binary_remote_addr 10m;  
  37. #多个站点设置,站点文件夹必须放在Nginx的html目录,否则不支持多站点  
  38. server {  
  39. listen 80;  
  40. server_name www.v-ec.com;  
  41. root /usr/local/webserver/nginx/html/www.v-ec.com;  
  42. index index.php index.html;  
  43. location ~ .*\.(php|php5)?$ {   
  44. #fastcgi_pass unix:/tmp/php-cgi.sock;  
  45. fastcgi_pass 127.0.0.1:9000;  
  46. fastcgi_index index.php;  
  47. include fcgi.conf;  
  48. }  
  49. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {  
  50. expires 30d;  
  51. }  
  52. location ~ .*\.(js|css)?$ {  
  53. expires 6h;  
  54. }  
  55. log_format veclog '$remote_addr - $remote_user [$time_local] 
    "$request" '  
  56. '$status $body_bytes_sent "$http_referer" '  
  57. '"$http_user_agent" $http_x_forwarded_for';  
  58. access_log logs/vec.log veclog;  
  59. }  
  60. #多个站点设置,站点文件夹必须放在Nginx的html目录,否则不支持多站点  
  61. server {  
  62. listen 80;  
  63. server_name www.w3cgroup.com;  
  64. root /usr/local/webserver/nginx/html/www.w3cgroup.com;  
  65. index index.php index.html;  
  66. location ~ .*\.(php|php5)?$ {   
  67. #fastcgi_pass unix:/tmp/php-cgi.sock;  
  68. fastcgi_pass 127.0.0.1:9000;  
  69. fastcgi_index index.php;  
  70. include fcgi.conf;  
  71. }  
  72. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {  
  73. expires 30d;  
  74. }  
  75. location ~ .*\.(js|css)?$ {  
  76. expires 6h;  
  77. }  
  78. log_format w3cclog '$remote_addr - $remote_user [$time_local] 
    "$request" '  
  79. '$status $body_bytes_sent "$http_referer" '  
  80. '"$http_user_agent" $http_x_forwarded_for';  
  81. access_log logs/w3c.log w3clog;  
  82. }  

 

九、配置开机自动启动Linux Nginx+ PHP

 

  1. vi /etc/rc.local 

在末尾增加以下内容:

 

  1. ulimit -SHn 51200  
  2. /usr/local/webserver/php/sbin/php-fpm start  
  3. /usr/local/webserver/nginx/sbin/nginx 

 


十、优化Linux内核参数

 

  1. vi /etc/sysctl.conf 

在末尾增加以下内容:

 

  1. net.ipv4.tcp_fin_timeout = 30 
  2. net.ipv4.tcp_keepalive_time = 300 
  3. net.ipv4.tcp_syncookies = 1 
  4. net.ipv4.tcp_tw_reuse = 1 
  5. net.ipv4.tcp_tw_recycle = 1 

 

使配置立即生效:

 

  1. /sbin/sysctl –p 

做到这里,可以reboot重启Linux了,不出意外的话应该可以顺利开启Linux Nginx。

【编辑推荐】

  1. Nginx Resin安装中的相关技巧和操作流程
  2. 深入学习有关Nginx负载均衡的安装过程
  3. Nginx 启动脚本如何进行自动化启动
  4. nginx配置进行数据输出的两种方式
  5. nginx内核参数相关的优化设定
责任编辑:张浩 来源: 互联网
相关推荐

2011-12-13 10:25:45

2022-01-14 14:33:20

安全挑战勒索软件供应链

2014-01-02 09:26:04

2013-03-28 15:09:29

2020-07-30 07:00:00

API安全威胁零日漏洞

2014-05-15 09:44:52

2011-08-22 16:45:49

2014-03-11 16:52:20

2013-07-05 10:18:14

2010-11-19 17:01:52

2016-12-26 16:23:24

2011-12-23 10:09:20

2016-02-15 14:32:38

2022-03-03 09:30:00

安全容器工具

2024-03-12 08:57:39

2013-07-26 13:23:28

2014-01-03 11:22:53

2018-08-31 08:11:29

2022-12-29 07:40:58

2024-04-28 09:47:32

Linux系统
点赞
收藏

51CTO技术栈公众号