First and foremost: Be involved in the class, perform at your level, and strive to improve (or demonstrate improvement. Being involved in class means that you participate in class discussions (while not dominating them), ask questions, help weaker students, and work well with your peers. Performing at your level means that you are consistently getting doing your work and getting the grades you are capable of getting; few things underwhelm teachers as much as seeing a student who is just phoning it in when they could easily be getting better grades. This also plays into striving to improve: you don't have to be the best student in the class to get a great recommendation. If you are seeking out the teacher to get extra help, working hard, and improving throughout the course, the teacher is more likely to go out of his/her way to write a good recommendation.
Aside from doing well in class, though, remember to ask a teacher at the end of your 11th grade year and get them to commit to writing the letter, but also provide them with more information about why you want the letter from them. Some teachers have forms to fill out, but if they don't, write them an email explaining why you liked their class, what your favorite activities were, what your favorite project was, what your favorite reading was, and also what you are interested studying in the future. If there are any circumstances that prevented you from doing as well as you could have--you had to work, take care of siblings, had family issues--make sure the teacher is aware.
If you make the effort and give them the appropriate amount of time, a teacher will write you a solid recommendation.